]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nht.c
Merge pull request #1756 from qlyoung/stylechecker
[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
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_nexthop.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_nht.h"
42 #include "bgpd/bgp_fsm.h"
43 #include "bgpd/bgp_zebra.h"
44
45 extern struct zclient *zclient;
46
47 static void register_zebra_rnh(struct bgp_nexthop_cache *bnc,
48 int is_bgp_static_route);
49 static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc,
50 int is_bgp_static_route);
51 static void evaluate_paths(struct bgp_nexthop_cache *bnc);
52 static int make_prefix(int afi, struct bgp_info *ri, struct prefix *p);
53 static void path_nh_map(struct bgp_info *path, struct bgp_nexthop_cache *bnc,
54 int keep);
55
56 static int bgp_isvalid_nexthop(struct bgp_nexthop_cache *bnc)
57 {
58 return (bgp_zebra_num_connects() == 0
59 || (bnc && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)));
60 }
61
62 int bgp_find_nexthop(struct bgp_info *path, int connected)
63 {
64 struct bgp_nexthop_cache *bnc = path->nexthop;
65
66 if (!bnc)
67 return 0;
68
69 /*
70 * We are cheating here. Views have no associated underlying
71 * ability to detect nexthops. So when we have a view
72 * just tell everyone the nexthop is valid
73 */
74 if (path->peer && path->peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
75 return 1;
76
77 if (connected && !(CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)))
78 return 0;
79
80 return (bgp_isvalid_nexthop(bnc));
81 }
82
83 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache *bnc)
84 {
85 if (LIST_EMPTY(&(bnc->paths)) && !bnc->nht_info) {
86 if (BGP_DEBUG(nht, NHT)) {
87 char buf[PREFIX2STR_BUFFER];
88 zlog_debug("bgp_unlink_nexthop: freeing bnc %s",
89 bnc_str(bnc, buf, PREFIX2STR_BUFFER));
90 }
91 unregister_zebra_rnh(bnc,
92 CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE));
93 bnc->node->info = NULL;
94 bgp_unlock_node(bnc->node);
95 bnc->node = NULL;
96 bnc_free(bnc);
97 }
98 }
99
100 void bgp_unlink_nexthop(struct bgp_info *path)
101 {
102 struct bgp_nexthop_cache *bnc = path->nexthop;
103
104 if (!bnc)
105 return;
106
107 path_nh_map(path, NULL, 0);
108
109 bgp_unlink_nexthop_check(bnc);
110 }
111
112 void bgp_unlink_nexthop_by_peer(struct peer *peer)
113 {
114 struct prefix p;
115 struct bgp_node *rn;
116 struct bgp_nexthop_cache *bnc;
117 afi_t afi = family2afi(peer->su.sa.sa_family);
118
119 if (!sockunion2hostprefix(&peer->su, &p))
120 return;
121
122 rn = bgp_node_get(peer->bgp->nexthop_cache_table[afi], &p);
123
124 if (!rn->info)
125 return;
126
127 bnc = rn->info;
128
129 /* cleanup the peer reference */
130 bnc->nht_info = NULL;
131
132 bgp_unlink_nexthop_check(bnc);
133 }
134
135 int bgp_find_or_add_nexthop(struct bgp *bgp, afi_t afi, struct bgp_info *ri,
136 struct peer *peer, int connected)
137 {
138 struct bgp_node *rn;
139 struct bgp_nexthop_cache *bnc;
140 struct prefix p;
141 int is_bgp_static_route = 0;
142
143 if (ri) {
144 is_bgp_static_route = ((ri->type == ZEBRA_ROUTE_BGP)
145 && (ri->sub_type == BGP_ROUTE_STATIC))
146 ? 1
147 : 0;
148
149 /* Since Extended Next-hop Encoding (RFC5549) support, we want
150 to derive
151 address-family from the next-hop. */
152 if (!is_bgp_static_route)
153 afi = BGP_ATTR_NEXTHOP_AFI_IP6(ri->attr) ? AFI_IP6
154 : AFI_IP;
155
156 /* This will return TRUE if the global IPv6 NH is a link local
157 * addr */
158 if (make_prefix(afi, ri, &p) < 0)
159 return 1;
160 } else if (peer) {
161 /* Don't register link local NH */
162 if (afi == AFI_IP6
163 && IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
164 return 1;
165
166 if (!sockunion2hostprefix(&peer->su, &p)) {
167 if (BGP_DEBUG(nht, NHT)) {
168 zlog_debug(
169 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
170 __FUNCTION__, afi, AFI_IP, AFI_IP6);
171 }
172 return 0;
173 }
174 } else
175 return 0;
176
177 if (is_bgp_static_route)
178 rn = bgp_node_get(bgp->import_check_table[afi], &p);
179 else
180 rn = bgp_node_get(bgp->nexthop_cache_table[afi], &p);
181
182 if (!rn->info) {
183 bnc = bnc_new();
184 rn->info = bnc;
185 bnc->node = rn;
186 bnc->bgp = bgp;
187 bgp_lock_node(rn);
188 if (BGP_DEBUG(nht, NHT)) {
189 char buf[PREFIX2STR_BUFFER];
190
191 zlog_debug("Allocated bnc %s peer %p",
192 bnc_str(bnc, buf, PREFIX2STR_BUFFER), peer);
193 }
194 }
195
196 bnc = rn->info;
197 bgp_unlock_node(rn);
198 if (is_bgp_static_route) {
199 SET_FLAG(bnc->flags, BGP_STATIC_ROUTE);
200
201 /* If we're toggling the type, re-register */
202 if ((bgp_flag_check(bgp, BGP_FLAG_IMPORT_CHECK))
203 && !CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)) {
204 SET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
205 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
206 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
207 } else if ((!bgp_flag_check(bgp, BGP_FLAG_IMPORT_CHECK))
208 && CHECK_FLAG(bnc->flags,
209 BGP_STATIC_ROUTE_EXACT_MATCH)) {
210 UNSET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
211 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
212 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
213 }
214 }
215 /* When nexthop is already known, but now requires 'connected'
216 * resolution,
217 * re-register it. The reverse scenario where the nexthop currently
218 * requires
219 * 'connected' resolution does not need a re-register (i.e., we treat
220 * 'connected-required' as an override) except in the scenario where
221 * this
222 * is actually a case of tracking a peer for connectivity (e.g., after
223 * disable connected-check).
224 * NOTE: We don't track the number of paths separately for 'connected-
225 * required' vs 'connected-not-required' as this change is not a common
226 * scenario.
227 */
228 else if (connected && !CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
229 SET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
230 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
231 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
232 } else if (peer && !connected
233 && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
234 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
235 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
236 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
237 }
238 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW) {
239 bnc->flags |= BGP_NEXTHOP_REGISTERED;
240 bnc->flags |= BGP_NEXTHOP_VALID;
241 } else if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED))
242 register_zebra_rnh(bnc, is_bgp_static_route);
243 if (ri && ri->nexthop != bnc) {
244 /* Unlink from existing nexthop cache, if any. This will also
245 * free
246 * the nexthop cache entry, if appropriate.
247 */
248 bgp_unlink_nexthop(ri);
249
250 path_nh_map(ri, bnc, 1); /* updates NHT ri list reference */
251
252 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID) && bnc->metric)
253 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
254 else if (ri->extra)
255 ri->extra->igpmetric = 0;
256 } else if (peer)
257 bnc->nht_info = (void *)peer; /* NHT peer reference */
258
259 /*
260 * We are cheating here. Views have no associated underlying
261 * ability to detect nexthops. So when we have a view
262 * just tell everyone the nexthop is valid
263 */
264 if (bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
265 return 1;
266 else
267 return (bgp_isvalid_nexthop(bnc));
268 }
269
270 void bgp_delete_connected_nexthop(afi_t afi, struct peer *peer)
271 {
272 struct bgp_node *rn;
273 struct bgp_nexthop_cache *bnc;
274 struct prefix p;
275
276 if (!peer)
277 return;
278
279 /* We don't register link local address for NHT */
280 if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
281 return;
282
283 if (!sockunion2hostprefix(&peer->su, &p))
284 return;
285
286 rn = bgp_node_lookup(
287 peer->bgp->nexthop_cache_table[family2afi(p.family)], &p);
288 if (!rn || !rn->info) {
289 if (BGP_DEBUG(nht, NHT))
290 zlog_debug("Cannot find connected NHT node for peer %s",
291 peer->host);
292 if (rn)
293 bgp_unlock_node(rn);
294 return;
295 }
296
297 bnc = rn->info;
298 bgp_unlock_node(rn);
299
300 if (bnc->nht_info != peer) {
301 if (BGP_DEBUG(nht, NHT))
302 zlog_debug(
303 "Connected NHT %p node for peer %s points to %p",
304 bnc, peer->host, bnc->nht_info);
305 return;
306 }
307
308 bnc->nht_info = NULL;
309
310 if (LIST_EMPTY(&(bnc->paths))) {
311 if (BGP_DEBUG(nht, NHT))
312 zlog_debug("Freeing connected NHT node %p for peer %s",
313 bnc, peer->host);
314 unregister_zebra_rnh(bnc, 0);
315 bnc->node->info = NULL;
316 bgp_unlock_node(bnc->node);
317 bnc_free(bnc);
318 }
319 }
320
321 void bgp_parse_nexthop_update(int command, vrf_id_t vrf_id)
322 {
323 struct bgp_node *rn = NULL;
324 struct bgp_nexthop_cache *bnc;
325 struct nexthop *nexthop;
326 struct nexthop *oldnh;
327 struct nexthop *nhlist_head = NULL;
328 struct nexthop *nhlist_tail = NULL;
329 int i;
330 struct bgp *bgp;
331 struct zapi_route nhr;
332
333 bgp = bgp_lookup_by_vrf_id(vrf_id);
334 if (!bgp) {
335 zlog_err(
336 "parse nexthop update: instance not found for vrf_id %u",
337 vrf_id);
338 return;
339 }
340
341 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
342 if (BGP_DEBUG(nht, NHT))
343 zlog_debug("%s: Failure to decode nexthop update",
344 __PRETTY_FUNCTION__);
345 return;
346 }
347
348 if (command == ZEBRA_NEXTHOP_UPDATE)
349 rn = bgp_node_lookup(
350 bgp->nexthop_cache_table[family2afi(nhr.prefix.family)],
351 &nhr.prefix);
352 else if (command == ZEBRA_IMPORT_CHECK_UPDATE)
353 rn = bgp_node_lookup(
354 bgp->import_check_table[family2afi(nhr.prefix.family)],
355 &nhr.prefix);
356
357 if (!rn || !rn->info) {
358 if (BGP_DEBUG(nht, NHT)) {
359 char buf[PREFIX2STR_BUFFER];
360 prefix2str(&nhr.prefix, buf, sizeof(buf));
361 zlog_debug("parse nexthop update(%s): rn not found",
362 buf);
363 }
364 if (rn)
365 bgp_unlock_node(rn);
366 return;
367 }
368
369 bnc = rn->info;
370 bgp_unlock_node(rn);
371 bnc->last_update = bgp_clock();
372 bnc->change_flags = 0;
373
374 /* debug print the input */
375 if (BGP_DEBUG(nht, NHT)) {
376 char buf[PREFIX2STR_BUFFER];
377 prefix2str(&nhr.prefix, buf, sizeof(buf));
378 zlog_debug(
379 "%u: Rcvd NH update %s - metric %d/%d #nhops %d/%d flags 0x%x",
380 vrf_id, buf, nhr.metric, bnc->metric, nhr.nexthop_num,
381 bnc->nexthop_num, bnc->flags);
382 }
383
384 if (nhr.metric != bnc->metric)
385 bnc->change_flags |= BGP_NEXTHOP_METRIC_CHANGED;
386
387 if (nhr.nexthop_num != bnc->nexthop_num)
388 bnc->change_flags |= BGP_NEXTHOP_CHANGED;
389
390 if (nhr.nexthop_num) {
391 /* notify bgp fsm if nbr ip goes from invalid->valid */
392 if (!bnc->nexthop_num)
393 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
394
395 bnc->flags |= BGP_NEXTHOP_VALID;
396 bnc->metric = nhr.metric;
397 bnc->nexthop_num = nhr.nexthop_num;
398
399 for (i = 0; i < nhr.nexthop_num; i++) {
400 nexthop = nexthop_from_zapi_nexthop(&nhr.nexthops[i]);
401
402 if (BGP_DEBUG(nht, NHT)) {
403 char buf[NEXTHOP_STRLEN];
404 zlog_debug(
405 " nhop via %s",
406 nexthop2str(nexthop, buf, sizeof(buf)));
407 }
408
409 if (nhlist_tail) {
410 nhlist_tail->next = nexthop;
411 nhlist_tail = nexthop;
412 } else {
413 nhlist_tail = nexthop;
414 nhlist_head = nexthop;
415 }
416
417 /* No need to evaluate the nexthop if we have already
418 * determined
419 * that there has been a change.
420 */
421 if (bnc->change_flags & BGP_NEXTHOP_CHANGED)
422 continue;
423
424 for (oldnh = bnc->nexthop; oldnh; oldnh = oldnh->next)
425 if (nexthop_same_no_recurse(oldnh, nexthop))
426 break;
427
428 if (!oldnh)
429 bnc->change_flags |= BGP_NEXTHOP_CHANGED;
430 }
431 bnc_nexthop_free(bnc);
432 bnc->nexthop = nhlist_head;
433 } else {
434 bnc->flags &= ~BGP_NEXTHOP_VALID;
435 bnc->nexthop_num = nhr.nexthop_num;
436
437 /* notify bgp fsm if nbr ip goes from valid->invalid */
438 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
439
440 bnc_nexthop_free(bnc);
441 bnc->nexthop = NULL;
442 }
443
444 evaluate_paths(bnc);
445 }
446
447 /*
448 * Cleanup nexthop registration and status information for BGP nexthops
449 * pertaining to this VRF. This is invoked upon VRF deletion.
450 */
451 void bgp_cleanup_nexthops(struct bgp *bgp)
452 {
453 afi_t afi;
454 struct bgp_node *rn;
455 struct bgp_nexthop_cache *bnc;
456
457 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
458 if (!bgp->nexthop_cache_table[afi])
459 continue;
460
461 for (rn = bgp_table_top(bgp->nexthop_cache_table[afi]); rn;
462 rn = bgp_route_next(rn)) {
463 bnc = rn->info;
464 if (!bnc)
465 continue;
466
467 /* Clear relevant flags. */
468 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
469 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
470 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
471 }
472 }
473 }
474
475 /**
476 * make_prefix - make a prefix structure from the path (essentially
477 * path's node.
478 */
479 static int make_prefix(int afi, struct bgp_info *ri, struct prefix *p)
480 {
481
482 int is_bgp_static = ((ri->type == ZEBRA_ROUTE_BGP)
483 && (ri->sub_type == BGP_ROUTE_STATIC))
484 ? 1
485 : 0;
486
487 memset(p, 0, sizeof(struct prefix));
488 switch (afi) {
489 case AFI_IP:
490 p->family = AF_INET;
491 if (is_bgp_static) {
492 p->u.prefix4 = ri->net->p.u.prefix4;
493 p->prefixlen = ri->net->p.prefixlen;
494 } else {
495 p->u.prefix4 = ri->attr->nexthop;
496 p->prefixlen = IPV4_MAX_BITLEN;
497 }
498 break;
499 case AFI_IP6:
500 /* We don't register link local NH */
501 if (ri->attr->mp_nexthop_len != BGP_ATTR_NHLEN_IPV6_GLOBAL
502 || IN6_IS_ADDR_LINKLOCAL(&ri->attr->mp_nexthop_global))
503 return -1;
504
505 p->family = AF_INET6;
506
507 if (is_bgp_static) {
508 p->u.prefix6 = ri->net->p.u.prefix6;
509 p->prefixlen = ri->net->p.prefixlen;
510 } else {
511 p->u.prefix6 = ri->attr->mp_nexthop_global;
512 p->prefixlen = IPV6_MAX_BITLEN;
513 }
514 break;
515 default:
516 if (BGP_DEBUG(nht, NHT)) {
517 zlog_debug(
518 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
519 __FUNCTION__, afi, AFI_IP, AFI_IP6);
520 }
521 break;
522 }
523 return 0;
524 }
525
526 /**
527 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
528 * command to Zebra.
529 * ARGUMENTS:
530 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
531 * int command -- command to send to zebra
532 * RETURNS:
533 * void.
534 */
535 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache *bnc, int command)
536 {
537 struct prefix *p;
538 bool exact_match = false;
539 int ret;
540
541 if (!zclient)
542 return;
543
544 /* Don't try to register if Zebra doesn't know of this instance. */
545 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc->bgp))
546 return;
547
548 p = &(bnc->node->p);
549 if ((command == ZEBRA_NEXTHOP_REGISTER ||
550 command == ZEBRA_IMPORT_ROUTE_REGISTER) &&
551 (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)
552 || CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)))
553 exact_match = true;
554
555 ret = zclient_send_rnh(zclient, command, p,
556 exact_match, bnc->bgp->vrf_id);
557 /* TBD: handle the failure */
558 if (ret < 0)
559 zlog_warn("sendmsg_nexthop: zclient_send_message() failed");
560
561 if ((command == ZEBRA_NEXTHOP_REGISTER)
562 || (command == ZEBRA_IMPORT_ROUTE_REGISTER))
563 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
564 else if ((command == ZEBRA_NEXTHOP_UNREGISTER)
565 || (command == ZEBRA_IMPORT_ROUTE_UNREGISTER))
566 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
567 return;
568 }
569
570 /**
571 * register_zebra_rnh - register a NH/route with Zebra for notification
572 * when the route or the route to the nexthop changes.
573 * ARGUMENTS:
574 * struct bgp_nexthop_cache *bnc
575 * RETURNS:
576 * void.
577 */
578 static void register_zebra_rnh(struct bgp_nexthop_cache *bnc,
579 int is_bgp_import_route)
580 {
581 /* Check if we have already registered */
582 if (bnc->flags & BGP_NEXTHOP_REGISTERED)
583 return;
584 if (is_bgp_import_route)
585 sendmsg_zebra_rnh(bnc, ZEBRA_IMPORT_ROUTE_REGISTER);
586 else
587 sendmsg_zebra_rnh(bnc, ZEBRA_NEXTHOP_REGISTER);
588 }
589
590 /**
591 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
592 * ARGUMENTS:
593 * struct bgp_nexthop_cache *bnc
594 * RETURNS:
595 * void.
596 */
597 static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc,
598 int is_bgp_import_route)
599 {
600 /* Check if we have already registered */
601 if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED))
602 return;
603
604 if (is_bgp_import_route)
605 sendmsg_zebra_rnh(bnc, ZEBRA_IMPORT_ROUTE_UNREGISTER);
606 else
607 sendmsg_zebra_rnh(bnc, ZEBRA_NEXTHOP_UNREGISTER);
608 }
609
610 /**
611 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
612 * ARGUMENTS:
613 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
614 * RETURNS:
615 * void.
616 */
617 static void evaluate_paths(struct bgp_nexthop_cache *bnc)
618 {
619 struct bgp_node *rn;
620 struct bgp_info *path;
621 struct bgp *bgp = bnc->bgp;
622 int afi;
623 struct peer *peer = (struct peer *)bnc->nht_info;
624 struct bgp_table *table;
625 safi_t safi;
626
627 if (BGP_DEBUG(nht, NHT)) {
628 char buf[PREFIX2STR_BUFFER];
629 bnc_str(bnc, buf, PREFIX2STR_BUFFER);
630 zlog_debug(
631 "NH update for %s - flags 0x%x chgflags 0x%x - evaluate paths",
632 buf, bnc->flags, bnc->change_flags);
633 }
634
635 LIST_FOREACH (path, &(bnc->paths), nh_thread) {
636 if (!(path->type == ZEBRA_ROUTE_BGP
637 && ((path->sub_type == BGP_ROUTE_NORMAL)
638 || (path->sub_type == BGP_ROUTE_STATIC))))
639 continue;
640
641 rn = path->net;
642 assert(rn && bgp_node_table(rn));
643 afi = family2afi(rn->p.family);
644 table = bgp_node_table(rn);
645 safi = table->safi;
646
647 /* Path becomes valid/invalid depending on whether the nexthop
648 * reachable/unreachable.
649 */
650 if ((CHECK_FLAG(path->flags, BGP_INFO_VALID) ? 1 : 0)
651 != (bgp_isvalid_nexthop(bnc) ? 1 : 0)) {
652 if (CHECK_FLAG(path->flags, BGP_INFO_VALID)) {
653 bgp_aggregate_decrement(bgp, &rn->p, path, afi,
654 safi);
655 bgp_info_unset_flag(rn, path, BGP_INFO_VALID);
656 } else {
657 bgp_info_set_flag(rn, path, BGP_INFO_VALID);
658 bgp_aggregate_increment(bgp, &rn->p, path, afi,
659 safi);
660 }
661 }
662
663 /* Copy the metric to the path. Will be used for bestpath
664 * computation */
665 if (bgp_isvalid_nexthop(bnc) && bnc->metric)
666 (bgp_info_extra_get(path))->igpmetric = bnc->metric;
667 else if (path->extra)
668 path->extra->igpmetric = 0;
669
670 if (CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_METRIC_CHANGED)
671 || CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED))
672 SET_FLAG(path->flags, BGP_INFO_IGP_CHANGED);
673
674 bgp_process(bgp, rn, afi, safi);
675 }
676
677 if (peer && !CHECK_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED)) {
678 if (BGP_DEBUG(nht, NHT))
679 zlog_debug("%s: Updating peer (%s) status with NHT",
680 __FUNCTION__, peer->host);
681 bgp_fsm_nht_update(peer, bgp_isvalid_nexthop(bnc));
682 SET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
683 }
684
685 RESET_FLAG(bnc->change_flags);
686 }
687
688 /**
689 * path_nh_map - make or break path-to-nexthop association.
690 * ARGUMENTS:
691 * path - pointer to the path structure
692 * bnc - pointer to the nexthop structure
693 * make - if set, make the association. if unset, just break the existing
694 * association.
695 */
696 static void path_nh_map(struct bgp_info *path, struct bgp_nexthop_cache *bnc,
697 int make)
698 {
699 if (path->nexthop) {
700 LIST_REMOVE(path, nh_thread);
701 path->nexthop->path_count--;
702 path->nexthop = NULL;
703 }
704 if (make) {
705 LIST_INSERT_HEAD(&(bnc->paths), path, nh_thread);
706 path->nexthop = bnc;
707 path->nexthop->path_count++;
708 }
709 }