]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nht.c
Merge pull request #8942 from ton31337/fix/cleanups_2
[mirror_frr.git] / bgpd / bgp_nht.c
1 /* BGP Nexthop tracking
2 * Copyright (C) 2013 Cumulus Networks, Inc.
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "thread.h"
25 #include "prefix.h"
26 #include "zclient.h"
27 #include "stream.h"
28 #include "network.h"
29 #include "log.h"
30 #include "memory.h"
31 #include "nexthop.h"
32 #include "vrf.h"
33 #include "filter.h"
34 #include "nexthop_group.h"
35
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_table.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_nexthop.h"
41 #include "bgpd/bgp_debug.h"
42 #include "bgpd/bgp_errors.h"
43 #include "bgpd/bgp_nht.h"
44 #include "bgpd/bgp_fsm.h"
45 #include "bgpd/bgp_zebra.h"
46 #include "bgpd/bgp_flowspec_util.h"
47 #include "bgpd/bgp_evpn.h"
48 #include "bgpd/bgp_rd.h"
49
50 extern struct zclient *zclient;
51
52 static void register_zebra_rnh(struct bgp_nexthop_cache *bnc,
53 int is_bgp_static_route);
54 static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc,
55 int is_bgp_static_route);
56 static int make_prefix(int afi, struct bgp_path_info *pi, struct prefix *p);
57 static int bgp_nht_ifp_initial(struct thread *thread);
58
59 static int bgp_isvalid_nexthop(struct bgp_nexthop_cache *bnc)
60 {
61 return (bgp_zebra_num_connects() == 0
62 || (bnc && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)
63 && bnc->nexthop_num > 0));
64 }
65
66 static int bgp_isvalid_labeled_nexthop(struct bgp_nexthop_cache *bnc)
67 {
68 /*
69 * In the case of MPLS-VPN, the label is learned from LDP or other
70 * protocols, and nexthop tracking is enabled for the label.
71 * The value is recorded as BGP_NEXTHOP_LABELED_VALID.
72 * In the case of SRv6-VPN, we need to track the reachability to the
73 * SID (in other words, IPv6 address). As in MPLS, we need to record
74 * the value as BGP_NEXTHOP_SID_VALID. However, this function is
75 * currently not implemented, and this function assumes that all
76 * Transit routes for SRv6-VPN are valid.
77 */
78 return (bgp_zebra_num_connects() == 0
79 || (bnc && bnc->nexthop_num > 0
80 && (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID)
81 || bnc->bgp->srv6_enabled)));
82 }
83
84 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache *bnc)
85 {
86 if (LIST_EMPTY(&(bnc->paths)) && !bnc->nht_info) {
87 if (BGP_DEBUG(nht, NHT)) {
88 char buf[PREFIX2STR_BUFFER];
89 zlog_debug("%s: freeing bnc %s(%u)(%s)", __func__,
90 bnc_str(bnc, buf, PREFIX2STR_BUFFER),
91 bnc->srte_color, bnc->bgp->name_pretty);
92 }
93 /* only unregister if this is the last nh for this prefix*/
94 if (!bnc_existing_for_prefix(bnc))
95 unregister_zebra_rnh(
96 bnc, CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE));
97 bnc_free(bnc);
98 }
99 }
100
101 void bgp_unlink_nexthop(struct bgp_path_info *path)
102 {
103 struct bgp_nexthop_cache *bnc = path->nexthop;
104
105 if (!bnc)
106 return;
107
108 path_nh_map(path, NULL, false);
109
110 bgp_unlink_nexthop_check(bnc);
111 }
112
113 void bgp_replace_nexthop_by_peer(struct peer *from, struct peer *to)
114 {
115 struct prefix pp;
116 struct prefix pt;
117 struct bgp_nexthop_cache *bncp, *bnct;
118 afi_t afi;
119
120 if (!sockunion2hostprefix(&from->su, &pp))
121 return;
122
123 afi = family2afi(pp.family);
124 bncp = bnc_find(&from->bgp->nexthop_cache_table[afi], &pp, 0);
125
126 if (!sockunion2hostprefix(&to->su, &pt))
127 return;
128
129 bnct = bnc_find(&to->bgp->nexthop_cache_table[afi], &pt, 0);
130
131 if (bnct != bncp)
132 return;
133
134 if (bnct)
135 bnct->nht_info = to;
136 }
137
138 void bgp_unlink_nexthop_by_peer(struct peer *peer)
139 {
140 struct prefix p;
141 struct bgp_nexthop_cache *bnc;
142 afi_t afi = family2afi(peer->su.sa.sa_family);
143
144 if (!sockunion2hostprefix(&peer->su, &p))
145 return;
146
147 bnc = bnc_find(&peer->bgp->nexthop_cache_table[afi], &p, 0);
148 if (!bnc)
149 return;
150
151 /* cleanup the peer reference */
152 bnc->nht_info = NULL;
153
154 bgp_unlink_nexthop_check(bnc);
155 }
156
157 /*
158 * A route and its nexthop might belong to different VRFs. Therefore,
159 * we need both the bgp_route and bgp_nexthop pointers.
160 */
161 int bgp_find_or_add_nexthop(struct bgp *bgp_route, struct bgp *bgp_nexthop,
162 afi_t afi, safi_t safi, struct bgp_path_info *pi,
163 struct peer *peer, int connected)
164 {
165 struct bgp_nexthop_cache_head *tree = NULL;
166 struct bgp_nexthop_cache *bnc;
167 struct prefix p;
168 uint32_t srte_color = 0;
169 int is_bgp_static_route = 0;
170 ifindex_t ifindex = 0;
171
172 if (pi) {
173 is_bgp_static_route = ((pi->type == ZEBRA_ROUTE_BGP)
174 && (pi->sub_type == BGP_ROUTE_STATIC))
175 ? 1
176 : 0;
177
178 /* Since Extended Next-hop Encoding (RFC5549) support, we want
179 to derive
180 address-family from the next-hop. */
181 if (!is_bgp_static_route)
182 afi = BGP_ATTR_NEXTHOP_AFI_IP6(pi->attr) ? AFI_IP6
183 : AFI_IP;
184
185 /* Validation for the ipv4 mapped ipv6 nexthop. */
186 if (IS_MAPPED_IPV6(&pi->attr->mp_nexthop_global)) {
187 afi = AFI_IP;
188 }
189
190 /* This will return true if the global IPv6 NH is a link local
191 * addr */
192 if (make_prefix(afi, pi, &p) < 0)
193 return 1;
194
195 srte_color = pi->attr->srte_color;
196 } else if (peer) {
197 /*
198 * Gather the ifindex for if up/down events to be
199 * tagged into this fun
200 */
201 if (afi == AFI_IP6
202 && IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
203 ifindex = peer->su.sin6.sin6_scope_id;
204
205 if (!sockunion2hostprefix(&peer->su, &p)) {
206 if (BGP_DEBUG(nht, NHT)) {
207 zlog_debug(
208 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
209 __func__, afi, AFI_IP, AFI_IP6);
210 }
211 return 0;
212 }
213 } else
214 return 0;
215
216 if (is_bgp_static_route)
217 tree = &bgp_nexthop->import_check_table[afi];
218 else
219 tree = &bgp_nexthop->nexthop_cache_table[afi];
220
221 bnc = bnc_find(tree, &p, srte_color);
222 if (!bnc) {
223 bnc = bnc_new(tree, &p, srte_color);
224 bnc->bgp = bgp_nexthop;
225 bnc->ifindex = ifindex;
226 if (BGP_DEBUG(nht, NHT)) {
227 char buf[PREFIX2STR_BUFFER];
228
229 zlog_debug("Allocated bnc %s(%u)(%s) peer %p",
230 bnc_str(bnc, buf, PREFIX2STR_BUFFER),
231 bnc->srte_color, bnc->bgp->name_pretty,
232 peer);
233 }
234 } else {
235 if (BGP_DEBUG(nht, NHT)) {
236 char buf[PREFIX2STR_BUFFER];
237
238 zlog_debug(
239 "Found existing bnc %s(%s) flags 0x%x ifindex %d #paths %d peer %p",
240 bnc_str(bnc, buf, PREFIX2STR_BUFFER),
241 bnc->bgp->name_pretty, bnc->flags, bnc->ifindex,
242 bnc->path_count, bnc->nht_info);
243 }
244 }
245
246 if (pi && is_route_parent_evpn(pi))
247 bnc->is_evpn_gwip_nexthop = true;
248
249 if (is_bgp_static_route) {
250 SET_FLAG(bnc->flags, BGP_STATIC_ROUTE);
251
252 /* If we're toggling the type, re-register */
253 if ((CHECK_FLAG(bgp_route->flags, BGP_FLAG_IMPORT_CHECK))
254 && !CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)) {
255 SET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
256 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
257 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
258 } else if ((!CHECK_FLAG(bgp_route->flags,
259 BGP_FLAG_IMPORT_CHECK))
260 && CHECK_FLAG(bnc->flags,
261 BGP_STATIC_ROUTE_EXACT_MATCH)) {
262 UNSET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
263 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
264 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
265 }
266 }
267 /* When nexthop is already known, but now requires 'connected'
268 * resolution,
269 * re-register it. The reverse scenario where the nexthop currently
270 * requires
271 * 'connected' resolution does not need a re-register (i.e., we treat
272 * 'connected-required' as an override) except in the scenario where
273 * this
274 * is actually a case of tracking a peer for connectivity (e.g., after
275 * disable connected-check).
276 * NOTE: We don't track the number of paths separately for 'connected-
277 * required' vs 'connected-not-required' as this change is not a common
278 * scenario.
279 */
280 else if (connected && !CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
281 SET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
282 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
283 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
284 } else if (peer && !connected
285 && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
286 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
287 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
288 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
289 }
290 if (peer && (bnc->ifindex != ifindex)) {
291 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
292 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
293 bnc->ifindex = ifindex;
294 }
295 if (bgp_route->inst_type == BGP_INSTANCE_TYPE_VIEW) {
296 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
297 SET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
298 } else if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED)
299 && !is_default_host_route(&bnc->prefix))
300 register_zebra_rnh(bnc, is_bgp_static_route);
301
302 if (pi && pi->nexthop != bnc) {
303 /* Unlink from existing nexthop cache, if any. This will also
304 * free
305 * the nexthop cache entry, if appropriate.
306 */
307 bgp_unlink_nexthop(pi);
308
309 /* updates NHT pi list reference */
310 path_nh_map(pi, bnc, true);
311
312 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID) && bnc->metric)
313 (bgp_path_info_extra_get(pi))->igpmetric = bnc->metric;
314 else if (pi->extra)
315 pi->extra->igpmetric = 0;
316 } else if (peer) {
317 /*
318 * Let's not accidently save the peer data for a peer
319 * we are going to throw away in a second or so.
320 * When we come back around we'll fix up this
321 * data properly in replace_nexthop_by_peer
322 */
323 if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
324 bnc->nht_info = (void *)peer; /* NHT peer reference */
325 }
326
327 /*
328 * We are cheating here. Views have no associated underlying
329 * ability to detect nexthops. So when we have a view
330 * just tell everyone the nexthop is valid
331 */
332 if (bgp_route->inst_type == BGP_INSTANCE_TYPE_VIEW)
333 return 1;
334 else if (safi == SAFI_UNICAST && pi
335 && pi->sub_type == BGP_ROUTE_IMPORTED && pi->extra
336 && pi->extra->num_labels && !bnc->is_evpn_gwip_nexthop) {
337 return bgp_isvalid_labeled_nexthop(bnc);
338 } else
339 return (bgp_isvalid_nexthop(bnc));
340 }
341
342 void bgp_delete_connected_nexthop(afi_t afi, struct peer *peer)
343 {
344 struct bgp_nexthop_cache *bnc;
345 struct prefix p;
346
347 if (!peer)
348 return;
349
350 if (!sockunion2hostprefix(&peer->su, &p))
351 return;
352
353 bnc = bnc_find(&peer->bgp->nexthop_cache_table[family2afi(p.family)],
354 &p, 0);
355 if (!bnc) {
356 if (BGP_DEBUG(nht, NHT))
357 zlog_debug(
358 "Cannot find connected NHT node for peer %s(%s)",
359 peer->host, peer->bgp->name_pretty);
360 return;
361 }
362
363 if (bnc->nht_info != peer) {
364 if (BGP_DEBUG(nht, NHT))
365 zlog_debug(
366 "Connected NHT %p node for peer %s(%s) points to %p",
367 bnc, peer->host, bnc->bgp->name_pretty,
368 bnc->nht_info);
369 return;
370 }
371
372 bnc->nht_info = NULL;
373
374 if (LIST_EMPTY(&(bnc->paths))) {
375 if (BGP_DEBUG(nht, NHT))
376 zlog_debug(
377 "Freeing connected NHT node %p for peer %s(%s)",
378 bnc, peer->host, bnc->bgp->name_pretty);
379 unregister_zebra_rnh(bnc, 0);
380 bnc_free(bnc);
381 }
382 }
383
384 static void bgp_process_nexthop_update(struct bgp_nexthop_cache *bnc,
385 struct zapi_route *nhr)
386 {
387 struct nexthop *nexthop;
388 struct nexthop *oldnh;
389 struct nexthop *nhlist_head = NULL;
390 struct nexthop *nhlist_tail = NULL;
391 int i;
392 bool evpn_resolved = false;
393
394 bnc->last_update = bgp_clock();
395 bnc->change_flags = 0;
396
397 /* debug print the input */
398 if (BGP_DEBUG(nht, NHT)) {
399 char bnc_buf[BNC_FLAG_DUMP_SIZE];
400
401 zlog_debug(
402 "%s(%u): Rcvd NH update %pFX(%u) - metric %d/%d #nhops %d/%d flags %s",
403 bnc->bgp->name_pretty, bnc->bgp->vrf_id, &nhr->prefix,
404 bnc->srte_color, nhr->metric, bnc->metric,
405 nhr->nexthop_num, bnc->nexthop_num,
406 bgp_nexthop_dump_bnc_flags(bnc, bnc_buf,
407 sizeof(bnc_buf)));
408 }
409
410 if (nhr->metric != bnc->metric)
411 bnc->change_flags |= BGP_NEXTHOP_METRIC_CHANGED;
412
413 if (nhr->nexthop_num != bnc->nexthop_num)
414 bnc->change_flags |= BGP_NEXTHOP_CHANGED;
415
416 if (nhr->nexthop_num) {
417 struct peer *peer = bnc->nht_info;
418
419 /* notify bgp fsm if nbr ip goes from invalid->valid */
420 if (!bnc->nexthop_num)
421 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
422
423 if (!bnc->is_evpn_gwip_nexthop)
424 bnc->flags |= BGP_NEXTHOP_VALID;
425 bnc->metric = nhr->metric;
426 bnc->nexthop_num = nhr->nexthop_num;
427
428 bnc->flags &= ~BGP_NEXTHOP_LABELED_VALID; /* check below */
429
430 for (i = 0; i < nhr->nexthop_num; i++) {
431 int num_labels = 0;
432
433 nexthop = nexthop_from_zapi_nexthop(&nhr->nexthops[i]);
434
435 /*
436 * Turn on RA for the v6 nexthops
437 * we receive from bgp. This is to allow us
438 * to work with v4 routing over v6 nexthops
439 */
440 if (peer && !peer->ifp
441 && CHECK_FLAG(peer->flags,
442 PEER_FLAG_CAPABILITY_ENHE)
443 && nhr->prefix.family == AF_INET6
444 && nexthop->type != NEXTHOP_TYPE_BLACKHOLE) {
445 struct interface *ifp;
446
447 ifp = if_lookup_by_index(nexthop->ifindex,
448 nexthop->vrf_id);
449 if (ifp)
450 zclient_send_interface_radv_req(
451 zclient, nexthop->vrf_id, ifp,
452 true,
453 BGP_UNNUM_DEFAULT_RA_INTERVAL);
454 }
455 /* There is at least one label-switched path */
456 if (nexthop->nh_label &&
457 nexthop->nh_label->num_labels) {
458
459 bnc->flags |= BGP_NEXTHOP_LABELED_VALID;
460 num_labels = nexthop->nh_label->num_labels;
461 }
462
463 if (BGP_DEBUG(nht, NHT)) {
464 char buf[NEXTHOP_STRLEN];
465 zlog_debug(
466 " nhop via %s (%d labels)",
467 nexthop2str(nexthop, buf, sizeof(buf)),
468 num_labels);
469 }
470
471 if (nhlist_tail) {
472 nhlist_tail->next = nexthop;
473 nhlist_tail = nexthop;
474 } else {
475 nhlist_tail = nexthop;
476 nhlist_head = nexthop;
477 }
478
479 /* No need to evaluate the nexthop if we have already
480 * determined
481 * that there has been a change.
482 */
483 if (bnc->change_flags & BGP_NEXTHOP_CHANGED)
484 continue;
485
486 for (oldnh = bnc->nexthop; oldnh; oldnh = oldnh->next)
487 if (nexthop_same(oldnh, nexthop))
488 break;
489
490 if (!oldnh)
491 bnc->change_flags |= BGP_NEXTHOP_CHANGED;
492 }
493 bnc_nexthop_free(bnc);
494 bnc->nexthop = nhlist_head;
495
496 /*
497 * Gateway IP nexthop is L3 reachable. Mark it as
498 * BGP_NEXTHOP_VALID only if it is recursively resolved with a
499 * remote EVPN RT-2.
500 * Else, mark it as BGP_NEXTHOP_EVPN_INCOMPLETE.
501 * When its mapping with EVPN RT-2 is established, unset
502 * BGP_NEXTHOP_EVPN_INCOMPLETE and set BGP_NEXTHOP_VALID.
503 */
504 if (bnc->is_evpn_gwip_nexthop) {
505 evpn_resolved = bgp_evpn_is_gateway_ip_resolved(bnc);
506
507 if (BGP_DEBUG(nht, NHT)) {
508 char buf2[PREFIX2STR_BUFFER];
509
510 prefix2str(&bnc->prefix, buf2, sizeof(buf2));
511 zlog_debug(
512 "EVPN gateway IP %s recursive MAC/IP lookup %s",
513 buf2,
514 (evpn_resolved ? "successful"
515 : "failed"));
516 }
517
518 if (evpn_resolved) {
519 bnc->flags |= BGP_NEXTHOP_VALID;
520 bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
521 bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
522 } else {
523 bnc->flags |= BGP_NEXTHOP_EVPN_INCOMPLETE;
524 bnc->flags &= ~BGP_NEXTHOP_VALID;
525 }
526 }
527 } else {
528 bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
529 bnc->flags &= ~BGP_NEXTHOP_VALID;
530 bnc->flags &= ~BGP_NEXTHOP_LABELED_VALID;
531 bnc->nexthop_num = nhr->nexthop_num;
532
533 /* notify bgp fsm if nbr ip goes from valid->invalid */
534 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
535
536 bnc_nexthop_free(bnc);
537 bnc->nexthop = NULL;
538 }
539
540 evaluate_paths(bnc);
541 }
542
543 static void bgp_nht_ifp_table_handle(struct bgp *bgp,
544 struct bgp_nexthop_cache_head *table,
545 struct interface *ifp, bool up)
546 {
547 struct bgp_nexthop_cache *bnc;
548
549 frr_each (bgp_nexthop_cache, table, bnc) {
550 if (bnc->ifindex != ifp->ifindex)
551 continue;
552
553 bnc->last_update = bgp_clock();
554 bnc->change_flags = 0;
555
556 /*
557 * For interface based routes ( ala the v6 LL routes
558 * that this was written for ) the metric received
559 * for the connected route is 0 not 1.
560 */
561 bnc->metric = 0;
562 if (up) {
563 SET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
564 SET_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED);
565 bnc->nexthop_num = 1;
566 } else {
567 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
568 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
569 SET_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED);
570 bnc->nexthop_num = 0;
571 }
572
573 evaluate_paths(bnc);
574 }
575 }
576 static void bgp_nht_ifp_handle(struct interface *ifp, bool up)
577 {
578 struct bgp *bgp;
579
580 bgp = bgp_lookup_by_vrf_id(ifp->vrf_id);
581 if (!bgp)
582 return;
583
584 bgp_nht_ifp_table_handle(bgp, &bgp->nexthop_cache_table[AFI_IP6], ifp,
585 up);
586 bgp_nht_ifp_table_handle(bgp, &bgp->import_check_table[AFI_IP6], ifp,
587 up);
588 }
589
590 void bgp_nht_ifp_up(struct interface *ifp)
591 {
592 bgp_nht_ifp_handle(ifp, true);
593 }
594
595 void bgp_nht_ifp_down(struct interface *ifp)
596 {
597 bgp_nht_ifp_handle(ifp, false);
598 }
599
600 static int bgp_nht_ifp_initial(struct thread *thread)
601 {
602 ifindex_t ifindex = THREAD_VAL(thread);
603 struct interface *ifp = if_lookup_by_index_all_vrf(ifindex);
604
605 if (!ifp)
606 return 0;
607
608 if (BGP_DEBUG(nht, NHT))
609 zlog_debug(
610 "Handle NHT initial update for Intf %s(%d) status %s",
611 ifp->name, ifp->ifindex, if_is_up(ifp) ? "up" : "down");
612
613 if (if_is_up(ifp))
614 bgp_nht_ifp_up(ifp);
615 else
616 bgp_nht_ifp_down(ifp);
617
618 return 0;
619 }
620
621 /*
622 * So the bnc code has the ability to handle interface up/down
623 * events to properly handle v6 LL peering.
624 * What is happening here:
625 * The event system for peering expects the nht code to
626 * report on the tracking events after we move to active
627 * So let's give the system a chance to report on that event
628 * in a manner that is expected.
629 */
630 void bgp_nht_interface_events(struct peer *peer)
631 {
632 struct bgp *bgp = peer->bgp;
633 struct bgp_nexthop_cache_head *table;
634 struct bgp_nexthop_cache *bnc;
635 struct prefix p;
636
637 if (!IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
638 return;
639
640 if (!sockunion2hostprefix(&peer->su, &p))
641 return;
642
643 table = &bgp->nexthop_cache_table[AFI_IP6];
644 bnc = bnc_find(table, &p, 0);
645 if (!bnc)
646 return;
647
648 if (bnc->ifindex)
649 thread_add_event(bm->master, bgp_nht_ifp_initial, NULL,
650 bnc->ifindex, NULL);
651 }
652
653 void bgp_parse_nexthop_update(int command, vrf_id_t vrf_id)
654 {
655 struct bgp_nexthop_cache_head *tree = NULL;
656 struct bgp_nexthop_cache *bnc;
657 struct bgp *bgp;
658 struct zapi_route nhr;
659 afi_t afi;
660
661 bgp = bgp_lookup_by_vrf_id(vrf_id);
662 if (!bgp) {
663 flog_err(
664 EC_BGP_NH_UPD,
665 "parse nexthop update: instance not found for vrf_id %u",
666 vrf_id);
667 return;
668 }
669
670 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
671 zlog_err("%s[%s]: Failure to decode nexthop update", __func__,
672 bgp->name_pretty);
673 return;
674 }
675
676 afi = family2afi(nhr.prefix.family);
677 if (command == ZEBRA_NEXTHOP_UPDATE)
678 tree = &bgp->nexthop_cache_table[afi];
679 else if (command == ZEBRA_IMPORT_CHECK_UPDATE)
680 tree = &bgp->import_check_table[afi];
681
682 bnc = bnc_find(tree, &nhr.prefix, nhr.srte_color);
683 if (!bnc) {
684 if (BGP_DEBUG(nht, NHT))
685 zlog_debug(
686 "parse nexthop update(%pFX(%u)(%s)): bnc info not found",
687 &nhr.prefix, nhr.srte_color, bgp->name_pretty);
688 return;
689 }
690
691 bgp_process_nexthop_update(bnc, &nhr);
692
693 /*
694 * HACK: if any BGP route is dependant on an SR-policy that doesn't
695 * exist, zebra will never send NH updates relative to that policy. In
696 * that case, whenever we receive an update about a colorless NH, update
697 * the corresponding colorful NHs that share the same endpoint but that
698 * are inactive. This ugly hack should work around the problem at the
699 * cost of a performance pernalty. Long term, what should be done is to
700 * make zebra's RNH subsystem aware of SR-TE colors (like bgpd is),
701 * which should provide a better infrastructure to solve this issue in
702 * a more efficient and elegant way.
703 */
704 if (nhr.srte_color == 0) {
705 struct bgp_nexthop_cache *bnc_iter;
706
707 frr_each (bgp_nexthop_cache, &bgp->nexthop_cache_table[afi],
708 bnc_iter) {
709 if (!prefix_same(&bnc->prefix, &bnc_iter->prefix)
710 || bnc_iter->srte_color == 0
711 || CHECK_FLAG(bnc_iter->flags, BGP_NEXTHOP_VALID))
712 continue;
713
714 bgp_process_nexthop_update(bnc_iter, &nhr);
715 }
716 }
717 }
718
719 /*
720 * Cleanup nexthop registration and status information for BGP nexthops
721 * pertaining to this VRF. This is invoked upon VRF deletion.
722 */
723 void bgp_cleanup_nexthops(struct bgp *bgp)
724 {
725 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++) {
726 struct bgp_nexthop_cache *bnc;
727
728 frr_each (bgp_nexthop_cache, &bgp->nexthop_cache_table[afi],
729 bnc) {
730 /* Clear relevant flags. */
731 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
732 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
733 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
734 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE);
735 }
736 }
737 }
738
739 /**
740 * make_prefix - make a prefix structure from the path (essentially
741 * path's node.
742 */
743 static int make_prefix(int afi, struct bgp_path_info *pi, struct prefix *p)
744 {
745
746 int is_bgp_static = ((pi->type == ZEBRA_ROUTE_BGP)
747 && (pi->sub_type == BGP_ROUTE_STATIC))
748 ? 1
749 : 0;
750 struct bgp_dest *net = pi->net;
751 const struct prefix *p_orig = bgp_dest_get_prefix(net);
752 struct in_addr ipv4;
753
754 if (p_orig->family == AF_FLOWSPEC) {
755 if (!pi->peer)
756 return -1;
757 return bgp_flowspec_get_first_nh(pi->peer->bgp,
758 pi, p, afi);
759 }
760 memset(p, 0, sizeof(struct prefix));
761 switch (afi) {
762 case AFI_IP:
763 p->family = AF_INET;
764 if (is_bgp_static) {
765 p->u.prefix4 = p_orig->u.prefix4;
766 p->prefixlen = p_orig->prefixlen;
767 } else {
768 if (IS_MAPPED_IPV6(&pi->attr->mp_nexthop_global)) {
769 ipv4_mapped_ipv6_to_ipv4(
770 &pi->attr->mp_nexthop_global, &ipv4);
771 p->u.prefix4 = ipv4;
772 p->prefixlen = IPV4_MAX_BITLEN;
773 } else {
774 p->u.prefix4 = pi->attr->nexthop;
775 p->prefixlen = IPV4_MAX_BITLEN;
776 }
777 }
778 break;
779 case AFI_IP6:
780 p->family = AF_INET6;
781
782 if (is_bgp_static) {
783 p->u.prefix6 = p_orig->u.prefix6;
784 p->prefixlen = p_orig->prefixlen;
785 } else {
786 /* If we receive MP_REACH nexthop with ::(LL)
787 * or LL(LL), use LL address as nexthop cache.
788 */
789 if (pi->attr->mp_nexthop_len
790 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
791 && (IN6_IS_ADDR_UNSPECIFIED(
792 &pi->attr->mp_nexthop_global)
793 || IN6_IS_ADDR_LINKLOCAL(
794 &pi->attr->mp_nexthop_global)))
795 p->u.prefix6 = pi->attr->mp_nexthop_local;
796 /* If we receive MR_REACH with (GA)::(LL)
797 * then check for route-map to choose GA or LL
798 */
799 else if (pi->attr->mp_nexthop_len
800 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
801 if (pi->attr->mp_nexthop_prefer_global)
802 p->u.prefix6 =
803 pi->attr->mp_nexthop_global;
804 else
805 p->u.prefix6 =
806 pi->attr->mp_nexthop_local;
807 } else
808 p->u.prefix6 = pi->attr->mp_nexthop_global;
809 p->prefixlen = IPV6_MAX_BITLEN;
810 }
811 break;
812 default:
813 if (BGP_DEBUG(nht, NHT)) {
814 zlog_debug(
815 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
816 __func__, afi, AFI_IP, AFI_IP6);
817 }
818 break;
819 }
820 return 0;
821 }
822
823 /**
824 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
825 * command to Zebra.
826 * ARGUMENTS:
827 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
828 * int command -- command to send to zebra
829 * RETURNS:
830 * void.
831 */
832 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache *bnc, int command)
833 {
834 bool exact_match = false;
835 int ret;
836
837 if (!zclient)
838 return;
839
840 /* Don't try to register if Zebra doesn't know of this instance. */
841 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc->bgp)) {
842 if (BGP_DEBUG(zebra, ZEBRA))
843 zlog_debug(
844 "%s: No zebra instance to talk to, not installing NHT entry",
845 __func__);
846 return;
847 }
848
849 if (!bgp_zebra_num_connects()) {
850 if (BGP_DEBUG(zebra, ZEBRA))
851 zlog_debug(
852 "%s: We have not connected yet, cannot send nexthops",
853 __func__);
854 }
855 if ((command == ZEBRA_NEXTHOP_REGISTER
856 || command == ZEBRA_IMPORT_ROUTE_REGISTER)
857 && (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)
858 || CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)))
859 exact_match = true;
860
861 if (BGP_DEBUG(zebra, ZEBRA))
862 zlog_debug("%s: sending cmd %s for %pFX (vrf %s)", __func__,
863 zserv_command_string(command), &bnc->prefix,
864 bnc->bgp->name_pretty);
865
866 ret = zclient_send_rnh(zclient, command, &bnc->prefix, exact_match,
867 bnc->bgp->vrf_id);
868 /* TBD: handle the failure */
869 if (ret == ZCLIENT_SEND_FAILURE)
870 flog_warn(EC_BGP_ZEBRA_SEND,
871 "sendmsg_nexthop: zclient_send_message() failed");
872
873 if ((command == ZEBRA_NEXTHOP_REGISTER)
874 || (command == ZEBRA_IMPORT_ROUTE_REGISTER))
875 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
876 else if ((command == ZEBRA_NEXTHOP_UNREGISTER)
877 || (command == ZEBRA_IMPORT_ROUTE_UNREGISTER))
878 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
879 return;
880 }
881
882 /**
883 * register_zebra_rnh - register a NH/route with Zebra for notification
884 * when the route or the route to the nexthop changes.
885 * ARGUMENTS:
886 * struct bgp_nexthop_cache *bnc
887 * RETURNS:
888 * void.
889 */
890 static void register_zebra_rnh(struct bgp_nexthop_cache *bnc,
891 int is_bgp_import_route)
892 {
893 /* Check if we have already registered */
894 if (bnc->flags & BGP_NEXTHOP_REGISTERED)
895 return;
896
897 if (bnc->ifindex) {
898 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
899 return;
900 }
901
902 if (is_bgp_import_route)
903 sendmsg_zebra_rnh(bnc, ZEBRA_IMPORT_ROUTE_REGISTER);
904 else
905 sendmsg_zebra_rnh(bnc, ZEBRA_NEXTHOP_REGISTER);
906 }
907
908 /**
909 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
910 * ARGUMENTS:
911 * struct bgp_nexthop_cache *bnc
912 * RETURNS:
913 * void.
914 */
915 static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc,
916 int is_bgp_import_route)
917 {
918 /* Check if we have already registered */
919 if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED))
920 return;
921
922 if (bnc->ifindex) {
923 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
924 return;
925 }
926
927 if (is_bgp_import_route)
928 sendmsg_zebra_rnh(bnc, ZEBRA_IMPORT_ROUTE_UNREGISTER);
929 else
930 sendmsg_zebra_rnh(bnc, ZEBRA_NEXTHOP_UNREGISTER);
931 }
932
933 /**
934 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
935 * ARGUMENTS:
936 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
937 * RETURNS:
938 * void.
939 */
940 void evaluate_paths(struct bgp_nexthop_cache *bnc)
941 {
942 struct bgp_dest *dest;
943 struct bgp_path_info *path;
944 int afi;
945 struct peer *peer = (struct peer *)bnc->nht_info;
946 struct bgp_table *table;
947 safi_t safi;
948 struct bgp *bgp_path;
949 const struct prefix *p;
950
951 if (BGP_DEBUG(nht, NHT)) {
952 char buf[PREFIX2STR_BUFFER];
953 char bnc_buf[BNC_FLAG_DUMP_SIZE];
954 char chg_buf[BNC_FLAG_DUMP_SIZE];
955
956 bnc_str(bnc, buf, PREFIX2STR_BUFFER);
957 zlog_debug(
958 "NH update for %s(%u)(%s) - flags %s chgflags %s- evaluate paths",
959 buf, bnc->srte_color, bnc->bgp->name_pretty,
960 bgp_nexthop_dump_bnc_flags(bnc, bnc_buf,
961 sizeof(bnc_buf)),
962 bgp_nexthop_dump_bnc_change_flags(bnc, chg_buf,
963 sizeof(bnc_buf)));
964 }
965
966 LIST_FOREACH (path, &(bnc->paths), nh_thread) {
967 if (!(path->type == ZEBRA_ROUTE_BGP
968 && ((path->sub_type == BGP_ROUTE_NORMAL)
969 || (path->sub_type == BGP_ROUTE_STATIC)
970 || (path->sub_type == BGP_ROUTE_IMPORTED))))
971 continue;
972
973 dest = path->net;
974 assert(dest && bgp_dest_table(dest));
975 p = bgp_dest_get_prefix(dest);
976 afi = family2afi(p->family);
977 table = bgp_dest_table(dest);
978 safi = table->safi;
979
980 /*
981 * handle routes from other VRFs (they can have a
982 * nexthop in THIS VRF). bgp_path is the bgp instance
983 * that owns the route referencing this nexthop.
984 */
985 bgp_path = table->bgp;
986
987 /*
988 * Path becomes valid/invalid depending on whether the nexthop
989 * reachable/unreachable.
990 *
991 * In case of unicast routes that were imported from vpn
992 * and that have labels, they are valid only if there are
993 * nexthops with labels
994 *
995 * If the nexthop is EVPN gateway-IP,
996 * do not check for a valid label.
997 */
998
999 bool bnc_is_valid_nexthop = false;
1000 bool path_valid = false;
1001
1002 if (safi == SAFI_UNICAST && path->sub_type == BGP_ROUTE_IMPORTED
1003 && path->extra && path->extra->num_labels
1004 && (path->attr->evpn_overlay.type
1005 != OVERLAY_INDEX_GATEWAY_IP)) {
1006 bnc_is_valid_nexthop =
1007 bgp_isvalid_labeled_nexthop(bnc) ? true : false;
1008 } else {
1009 if (bgp_update_martian_nexthop(
1010 bnc->bgp, afi, safi, path->type,
1011 path->sub_type, path->attr, dest)) {
1012 if (BGP_DEBUG(nht, NHT))
1013 zlog_debug(
1014 "%s: prefix %pBD (vrf %s), ignoring path due to martian or self-next-hop",
1015 __func__, dest, bgp_path->name);
1016 } else
1017 bnc_is_valid_nexthop =
1018 bgp_isvalid_nexthop(bnc) ? true : false;
1019 }
1020
1021 if (BGP_DEBUG(nht, NHT)) {
1022 char buf1[RD_ADDRSTRLEN];
1023
1024 if (dest->pdest) {
1025 prefix_rd2str((struct prefix_rd *)bgp_dest_get_prefix(dest->pdest),
1026 buf1, sizeof(buf1));
1027 zlog_debug(
1028 "... eval path %d/%d %pBD RD %s %s flags 0x%x",
1029 afi, safi, dest, buf1,
1030 bgp_path->name_pretty, path->flags);
1031 } else
1032 zlog_debug(
1033 "... eval path %d/%d %pBD %s flags 0x%x",
1034 afi, safi, dest, bgp_path->name_pretty,
1035 path->flags);
1036 }
1037
1038 /* Skip paths marked for removal or as history. */
1039 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED)
1040 || CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
1041 continue;
1042
1043 /* Copy the metric to the path. Will be used for bestpath
1044 * computation */
1045 if (bgp_isvalid_nexthop(bnc) && bnc->metric)
1046 (bgp_path_info_extra_get(path))->igpmetric =
1047 bnc->metric;
1048 else if (path->extra)
1049 path->extra->igpmetric = 0;
1050
1051 if (CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_METRIC_CHANGED)
1052 || CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED)
1053 || path->attr->srte_color != 0)
1054 SET_FLAG(path->flags, BGP_PATH_IGP_CHANGED);
1055
1056 path_valid = CHECK_FLAG(path->flags, BGP_PATH_VALID);
1057 if (path_valid != bnc_is_valid_nexthop) {
1058 if (path_valid) {
1059 /* No longer valid, clear flag; also for EVPN
1060 * routes, unimport from VRFs if needed.
1061 */
1062 bgp_aggregate_decrement(bgp_path, p, path, afi,
1063 safi);
1064 bgp_path_info_unset_flag(dest, path,
1065 BGP_PATH_VALID);
1066 if (safi == SAFI_EVPN &&
1067 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest)))
1068 bgp_evpn_unimport_route(bgp_path,
1069 afi, safi, bgp_dest_get_prefix(dest), path);
1070 } else {
1071 /* Path becomes valid, set flag; also for EVPN
1072 * routes, import from VRFs if needed.
1073 */
1074 bgp_path_info_set_flag(dest, path,
1075 BGP_PATH_VALID);
1076 bgp_aggregate_increment(bgp_path, p, path, afi,
1077 safi);
1078 if (safi == SAFI_EVPN &&
1079 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest)))
1080 bgp_evpn_import_route(bgp_path,
1081 afi, safi, bgp_dest_get_prefix(dest), path);
1082 }
1083 }
1084
1085 bgp_process(bgp_path, dest, afi, safi);
1086 }
1087
1088 if (peer) {
1089 int valid_nexthops = bgp_isvalid_nexthop(bnc);
1090
1091 if (valid_nexthops) {
1092 /*
1093 * Peering cannot occur across a blackhole nexthop
1094 */
1095 if (bnc->nexthop_num == 1 && bnc->nexthop
1096 && bnc->nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
1097 peer->last_reset = PEER_DOWN_WAITING_NHT;
1098 valid_nexthops = 0;
1099 } else
1100 peer->last_reset = PEER_DOWN_WAITING_OPEN;
1101 } else
1102 peer->last_reset = PEER_DOWN_WAITING_NHT;
1103
1104 if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED)) {
1105 if (BGP_DEBUG(nht, NHT))
1106 zlog_debug(
1107 "%s: Updating peer (%s(%s)) status with NHT nexthops %d",
1108 __func__, peer->host,
1109 peer->bgp->name_pretty,
1110 !!valid_nexthops);
1111 bgp_fsm_nht_update(peer, !!valid_nexthops);
1112 SET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
1113 }
1114 }
1115
1116 RESET_FLAG(bnc->change_flags);
1117 }
1118
1119 /**
1120 * path_nh_map - make or break path-to-nexthop association.
1121 * ARGUMENTS:
1122 * path - pointer to the path structure
1123 * bnc - pointer to the nexthop structure
1124 * make - if set, make the association. if unset, just break the existing
1125 * association.
1126 */
1127 void path_nh_map(struct bgp_path_info *path, struct bgp_nexthop_cache *bnc,
1128 bool make)
1129 {
1130 if (path->nexthop) {
1131 LIST_REMOVE(path, nh_thread);
1132 path->nexthop->path_count--;
1133 path->nexthop = NULL;
1134 }
1135 if (make) {
1136 LIST_INSERT_HEAD(&(bnc->paths), path, nh_thread);
1137 path->nexthop = bnc;
1138 path->nexthop->path_count++;
1139 }
1140 }
1141
1142 /*
1143 * This function is called to register nexthops to zebra
1144 * as that we may have tried to install the nexthops
1145 * before we actually have a zebra connection
1146 */
1147 void bgp_nht_register_nexthops(struct bgp *bgp)
1148 {
1149 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++) {
1150 struct bgp_nexthop_cache *bnc;
1151
1152 frr_each (bgp_nexthop_cache, &bgp->nexthop_cache_table[afi],
1153 bnc) {
1154 register_zebra_rnh(bnc, 0);
1155 }
1156 }
1157 }
1158
1159 void bgp_nht_reg_enhe_cap_intfs(struct peer *peer)
1160 {
1161 struct bgp *bgp;
1162 struct bgp_nexthop_cache *bnc;
1163 struct nexthop *nhop;
1164 struct interface *ifp;
1165 struct prefix p;
1166
1167 if (peer->ifp)
1168 return;
1169
1170 bgp = peer->bgp;
1171 if (!sockunion2hostprefix(&peer->su, &p)) {
1172 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1173 __func__, peer->host);
1174 return;
1175 }
1176
1177 if (p.family != AF_INET6)
1178 return;
1179
1180 bnc = bnc_find(&bgp->nexthop_cache_table[AFI_IP6], &p, 0);
1181 if (!bnc)
1182 return;
1183
1184 if (peer != bnc->nht_info)
1185 return;
1186
1187 for (nhop = bnc->nexthop; nhop; nhop = nhop->next) {
1188 ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
1189
1190 if (!ifp)
1191 continue;
1192
1193 zclient_send_interface_radv_req(zclient,
1194 nhop->vrf_id,
1195 ifp, true,
1196 BGP_UNNUM_DEFAULT_RA_INTERVAL);
1197 }
1198 }
1199
1200 void bgp_nht_dereg_enhe_cap_intfs(struct peer *peer)
1201 {
1202 struct bgp *bgp;
1203 struct bgp_nexthop_cache *bnc;
1204 struct nexthop *nhop;
1205 struct interface *ifp;
1206 struct prefix p;
1207
1208 if (peer->ifp)
1209 return;
1210
1211 bgp = peer->bgp;
1212
1213 if (!sockunion2hostprefix(&peer->su, &p)) {
1214 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1215 __func__, peer->host);
1216 return;
1217 }
1218
1219 if (p.family != AF_INET6)
1220 return;
1221
1222 bnc = bnc_find(&bgp->nexthop_cache_table[AFI_IP6], &p, 0);
1223 if (!bnc)
1224 return;
1225
1226 if (peer != bnc->nht_info)
1227 return;
1228
1229 for (nhop = bnc->nexthop; nhop; nhop = nhop->next) {
1230 ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
1231
1232 if (!ifp)
1233 continue;
1234
1235 zclient_send_interface_radv_req(zclient, nhop->vrf_id, ifp, 0,
1236 0);
1237 }
1238 }
1239
1240 /****************************************************************************
1241 * L3 NHGs are used for fast failover of nexthops in the dplane. These are
1242 * the APIs for allocating L3 NHG ids. Management of the L3 NHG itself is
1243 * left to the application using it.
1244 * PS: Currently EVPN host routes is the only app using L3 NHG for fast
1245 * failover of remote ES links.
1246 ***************************************************************************/
1247 static bitfield_t bgp_nh_id_bitmap;
1248 static uint32_t bgp_l3nhg_start;
1249
1250 /* XXX - currently we do nothing on the callbacks */
1251 static void bgp_l3nhg_add_cb(const char *name)
1252 {
1253 }
1254 static void bgp_l3nhg_add_nexthop_cb(const struct nexthop_group_cmd *nhgc,
1255 const struct nexthop *nhop)
1256 {
1257 }
1258 static void bgp_l3nhg_del_nexthop_cb(const struct nexthop_group_cmd *nhgc,
1259 const struct nexthop *nhop)
1260 {
1261 }
1262 static void bgp_l3nhg_del_cb(const char *name)
1263 {
1264 }
1265
1266 static void bgp_l3nhg_zebra_init(void)
1267 {
1268 static bool bgp_l3nhg_zebra_inited;
1269 if (bgp_l3nhg_zebra_inited)
1270 return;
1271
1272 bgp_l3nhg_zebra_inited = true;
1273 bgp_l3nhg_start = zclient_get_nhg_start(ZEBRA_ROUTE_BGP);
1274 nexthop_group_init(bgp_l3nhg_add_cb, bgp_l3nhg_add_nexthop_cb,
1275 bgp_l3nhg_del_nexthop_cb, bgp_l3nhg_del_cb);
1276 }
1277
1278
1279 #define min(A, B) ((A) < (B) ? (A) : (B))
1280 void bgp_l3nhg_init(void)
1281 {
1282 uint32_t id_max;
1283
1284 id_max = min(ZEBRA_NHG_PROTO_SPACING - 1, 16 * 1024);
1285 bf_init(bgp_nh_id_bitmap, id_max);
1286 bf_assign_zero_index(bgp_nh_id_bitmap);
1287
1288 if (BGP_DEBUG(nht, NHT) || BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1289 zlog_debug("bgp l3_nhg range %u - %u", bgp_l3nhg_start + 1,
1290 bgp_l3nhg_start + id_max);
1291 }
1292
1293 void bgp_l3nhg_finish(void)
1294 {
1295 bf_free(bgp_nh_id_bitmap);
1296 }
1297
1298 uint32_t bgp_l3nhg_id_alloc(void)
1299 {
1300 uint32_t nhg_id = 0;
1301
1302 bgp_l3nhg_zebra_init();
1303 bf_assign_index(bgp_nh_id_bitmap, nhg_id);
1304 if (nhg_id)
1305 nhg_id += bgp_l3nhg_start;
1306
1307 return nhg_id;
1308 }
1309
1310 void bgp_l3nhg_id_free(uint32_t nhg_id)
1311 {
1312 if (!nhg_id || (nhg_id <= bgp_l3nhg_start))
1313 return;
1314
1315 nhg_id -= bgp_l3nhg_start;
1316
1317 bf_release_index(bgp_nh_id_bitmap, nhg_id);
1318 }