1 /* BGP routing information
2 * Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
3 * Copyright (C) 2016 Job Snijders <job@instituut.net>
5 * This file is part of GNU Zebra.
7 * GNU Zebra 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
12 * GNU Zebra 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.
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
34 #include "sockunion.h"
37 #include "workqueue.h"
41 #include "lib_errors.h"
43 #include "bgpd/bgpd.h"
44 #include "bgpd/bgp_table.h"
45 #include "bgpd/bgp_route.h"
46 #include "bgpd/bgp_attr.h"
47 #include "bgpd/bgp_debug.h"
48 #include "bgpd/bgp_errors.h"
49 #include "bgpd/bgp_aspath.h"
50 #include "bgpd/bgp_regex.h"
51 #include "bgpd/bgp_community.h"
52 #include "bgpd/bgp_ecommunity.h"
53 #include "bgpd/bgp_lcommunity.h"
54 #include "bgpd/bgp_clist.h"
55 #include "bgpd/bgp_packet.h"
56 #include "bgpd/bgp_filter.h"
57 #include "bgpd/bgp_fsm.h"
58 #include "bgpd/bgp_mplsvpn.h"
59 #include "bgpd/bgp_nexthop.h"
60 #include "bgpd/bgp_damp.h"
61 #include "bgpd/bgp_advertise.h"
62 #include "bgpd/bgp_zebra.h"
63 #include "bgpd/bgp_vty.h"
64 #include "bgpd/bgp_mpath.h"
65 #include "bgpd/bgp_nht.h"
66 #include "bgpd/bgp_updgrp.h"
67 #include "bgpd/bgp_label.h"
68 #include "bgpd/bgp_addpath.h"
69 #include "bgpd/bgp_mac.h"
72 #include "bgpd/rfapi/rfapi_backend.h"
73 #include "bgpd/rfapi/vnc_import_bgp.h"
74 #include "bgpd/rfapi/vnc_export_bgp.h"
76 #include "bgpd/bgp_encap_types.h"
77 #include "bgpd/bgp_encap_tlv.h"
78 #include "bgpd/bgp_evpn.h"
79 #include "bgpd/bgp_evpn_vty.h"
80 #include "bgpd/bgp_flowspec.h"
81 #include "bgpd/bgp_flowspec_util.h"
82 #include "bgpd/bgp_pbr.h"
84 #ifndef VTYSH_EXTRACT_PL
85 #include "bgpd/bgp_route_clippy.c"
88 /* Extern from bgp_dump.c */
89 extern const char *bgp_origin_str
[];
90 extern const char *bgp_origin_long_str
[];
93 #define PMSI_TNLTYPE_STR_NO_INFO "No info"
94 #define PMSI_TNLTYPE_STR_DEFAULT PMSI_TNLTYPE_STR_NO_INFO
95 static const struct message bgp_pmsi_tnltype_str
[] = {
96 {PMSI_TNLTYPE_NO_INFO
, PMSI_TNLTYPE_STR_NO_INFO
},
97 {PMSI_TNLTYPE_RSVP_TE_P2MP
, "RSVP-TE P2MP"},
98 {PMSI_TNLTYPE_MLDP_P2MP
, "mLDP P2MP"},
99 {PMSI_TNLTYPE_PIM_SSM
, "PIM-SSM"},
100 {PMSI_TNLTYPE_PIM_SM
, "PIM-SM"},
101 {PMSI_TNLTYPE_PIM_BIDIR
, "PIM-BIDIR"},
102 {PMSI_TNLTYPE_INGR_REPL
, "Ingress Replication"},
103 {PMSI_TNLTYPE_MLDP_MP2MP
, "mLDP MP2MP"},
107 #define VRFID_NONE_STR "-"
109 struct bgp_node
*bgp_afi_node_get(struct bgp_table
*table
, afi_t afi
,
110 safi_t safi
, struct prefix
*p
,
111 struct prefix_rd
*prd
)
114 struct bgp_node
*prn
= NULL
;
120 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
121 || (safi
== SAFI_EVPN
)) {
122 prn
= bgp_node_get(table
, (struct prefix
*)prd
);
124 if (!bgp_node_has_bgp_path_info_data(prn
))
125 bgp_node_set_bgp_table_info(
126 prn
, bgp_table_init(table
->bgp
, afi
, safi
));
128 bgp_unlock_node(prn
);
129 table
= bgp_node_get_bgp_table_info(prn
);
132 rn
= bgp_node_get(table
, p
);
134 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
135 || (safi
== SAFI_EVPN
))
141 struct bgp_node
*bgp_afi_node_lookup(struct bgp_table
*table
, afi_t afi
,
142 safi_t safi
, struct prefix
*p
,
143 struct prefix_rd
*prd
)
146 struct bgp_node
*prn
= NULL
;
151 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
152 || (safi
== SAFI_EVPN
)) {
153 prn
= bgp_node_lookup(table
, (struct prefix
*)prd
);
157 if (!bgp_node_has_bgp_path_info_data(prn
)) {
158 bgp_unlock_node(prn
);
162 table
= bgp_node_get_bgp_table_info(prn
);
165 rn
= bgp_node_lookup(table
, p
);
170 /* Allocate bgp_path_info_extra */
171 static struct bgp_path_info_extra
*bgp_path_info_extra_new(void)
173 struct bgp_path_info_extra
*new;
174 new = XCALLOC(MTYPE_BGP_ROUTE_EXTRA
,
175 sizeof(struct bgp_path_info_extra
));
176 new->label
[0] = MPLS_INVALID_LABEL
;
178 new->bgp_fs_pbr
= list_new();
179 new->bgp_fs_iprule
= list_new();
183 void bgp_path_info_extra_free(struct bgp_path_info_extra
**extra
)
185 struct bgp_path_info_extra
*e
;
187 if (!extra
|| !*extra
)
192 bgp_damp_info_free(e
->damp_info
, 0);
196 struct bgp_path_info
*bpi
= (struct bgp_path_info
*)e
->parent
;
199 /* FIXME: since multiple e may have the same e->parent
200 * and e->parent->net is holding a refcount for each
201 * of them, we need to do some fudging here.
203 * WARNING: if bpi->net->lock drops to 0, bpi may be
204 * freed as well (because bpi->net was holding the
205 * last reference to bpi) => write after free!
209 bpi
= bgp_path_info_lock(bpi
);
210 refcount
= bpi
->net
->lock
- 1;
211 bgp_unlock_node((struct bgp_node
*)bpi
->net
);
214 bgp_path_info_unlock(bpi
);
216 bgp_path_info_unlock(e
->parent
);
221 bgp_unlock(e
->bgp_orig
);
223 if ((*extra
)->bgp_fs_iprule
)
224 list_delete(&((*extra
)->bgp_fs_iprule
));
225 if ((*extra
)->bgp_fs_pbr
)
226 list_delete(&((*extra
)->bgp_fs_pbr
));
227 XFREE(MTYPE_BGP_ROUTE_EXTRA
, *extra
);
232 /* Get bgp_path_info extra information for the given bgp_path_info, lazy
233 * allocated if required.
235 struct bgp_path_info_extra
*bgp_path_info_extra_get(struct bgp_path_info
*pi
)
238 pi
->extra
= bgp_path_info_extra_new();
242 /* Allocate new bgp info structure. */
243 struct bgp_path_info
*bgp_path_info_new(void)
245 return XCALLOC(MTYPE_BGP_ROUTE
, sizeof(struct bgp_path_info
));
248 /* Free bgp route information. */
249 static void bgp_path_info_free(struct bgp_path_info
*path
)
252 bgp_attr_unintern(&path
->attr
);
254 bgp_unlink_nexthop(path
);
255 bgp_path_info_extra_free(&path
->extra
);
256 bgp_path_info_mpath_free(&path
->mpath
);
257 bgp_addpath_free_info_data(&path
->tx_addpath
,
258 path
->net
? &path
->net
->tx_addpath
: NULL
);
260 peer_unlock(path
->peer
); /* bgp_path_info peer reference */
262 XFREE(MTYPE_BGP_ROUTE
, path
);
265 struct bgp_path_info
*bgp_path_info_lock(struct bgp_path_info
*path
)
271 struct bgp_path_info
*bgp_path_info_unlock(struct bgp_path_info
*path
)
273 assert(path
&& path
->lock
> 0);
276 if (path
->lock
== 0) {
278 zlog_debug ("%s: unlocked and freeing", __func__
);
279 zlog_backtrace (LOG_DEBUG
);
281 bgp_path_info_free(path
);
288 zlog_debug ("%s: unlocked to 1", __func__
);
289 zlog_backtrace (LOG_DEBUG
);
296 void bgp_path_info_add(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
298 struct bgp_path_info
*top
;
300 top
= bgp_node_get_bgp_path_info(rn
);
306 bgp_node_set_bgp_path_info(rn
, pi
);
308 bgp_path_info_lock(pi
);
310 peer_lock(pi
->peer
); /* bgp_path_info peer reference */
313 /* Do the actual removal of info from RIB, for use by bgp_process
314 completion callback *only* */
315 void bgp_path_info_reap(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
318 pi
->next
->prev
= pi
->prev
;
320 pi
->prev
->next
= pi
->next
;
322 bgp_node_set_bgp_path_info(rn
, pi
->next
);
324 bgp_path_info_mpath_dequeue(pi
);
325 bgp_path_info_unlock(pi
);
329 void bgp_path_info_delete(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
331 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_REMOVED
);
332 /* set of previous already took care of pcount */
333 UNSET_FLAG(pi
->flags
, BGP_PATH_VALID
);
336 /* undo the effects of a previous call to bgp_path_info_delete; typically
337 called when a route is deleted and then quickly re-added before the
338 deletion has been processed */
339 void bgp_path_info_restore(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
341 bgp_path_info_unset_flag(rn
, pi
, BGP_PATH_REMOVED
);
342 /* unset of previous already took care of pcount */
343 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
346 /* Adjust pcount as required */
347 static void bgp_pcount_adjust(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
349 struct bgp_table
*table
;
351 assert(rn
&& bgp_node_table(rn
));
352 assert(pi
&& pi
->peer
&& pi
->peer
->bgp
);
354 table
= bgp_node_table(rn
);
356 if (pi
->peer
== pi
->peer
->bgp
->peer_self
)
359 if (!BGP_PATH_COUNTABLE(pi
)
360 && CHECK_FLAG(pi
->flags
, BGP_PATH_COUNTED
)) {
362 UNSET_FLAG(pi
->flags
, BGP_PATH_COUNTED
);
364 /* slight hack, but more robust against errors. */
365 if (pi
->peer
->pcount
[table
->afi
][table
->safi
])
366 pi
->peer
->pcount
[table
->afi
][table
->safi
]--;
368 flog_err(EC_LIB_DEVELOPMENT
,
369 "Asked to decrement 0 prefix count for peer");
370 } else if (BGP_PATH_COUNTABLE(pi
)
371 && !CHECK_FLAG(pi
->flags
, BGP_PATH_COUNTED
)) {
372 SET_FLAG(pi
->flags
, BGP_PATH_COUNTED
);
373 pi
->peer
->pcount
[table
->afi
][table
->safi
]++;
377 static int bgp_label_index_differs(struct bgp_path_info
*pi1
,
378 struct bgp_path_info
*pi2
)
380 return (!(pi1
->attr
->label_index
== pi2
->attr
->label_index
));
383 /* Set/unset bgp_path_info flags, adjusting any other state as needed.
384 * This is here primarily to keep prefix-count in check.
386 void bgp_path_info_set_flag(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
389 SET_FLAG(pi
->flags
, flag
);
391 /* early bath if we know it's not a flag that changes countability state
393 if (!CHECK_FLAG(flag
,
394 BGP_PATH_VALID
| BGP_PATH_HISTORY
| BGP_PATH_REMOVED
))
397 bgp_pcount_adjust(rn
, pi
);
400 void bgp_path_info_unset_flag(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
403 UNSET_FLAG(pi
->flags
, flag
);
405 /* early bath if we know it's not a flag that changes countability state
407 if (!CHECK_FLAG(flag
,
408 BGP_PATH_VALID
| BGP_PATH_HISTORY
| BGP_PATH_REMOVED
))
411 bgp_pcount_adjust(rn
, pi
);
414 /* Get MED value. If MED value is missing and "bgp bestpath
415 missing-as-worst" is specified, treat it as the worst value. */
416 static uint32_t bgp_med_value(struct attr
*attr
, struct bgp
*bgp
)
418 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
421 if (bgp_flag_check(bgp
, BGP_FLAG_MED_MISSING_AS_WORST
))
428 void bgp_path_info_path_with_addpath_rx_str(struct bgp_path_info
*pi
, char *buf
)
430 if (pi
->addpath_rx_id
)
431 sprintf(buf
, "path %s (addpath rxid %d)", pi
->peer
->host
,
434 sprintf(buf
, "path %s", pi
->peer
->host
);
437 /* Compare two bgp route entity. If 'new' is preferable over 'exist' return 1.
439 static int bgp_path_info_cmp(struct bgp
*bgp
, struct bgp_path_info
*new,
440 struct bgp_path_info
*exist
, int *paths_eq
,
441 struct bgp_maxpaths_cfg
*mpath_cfg
, int debug
,
442 char *pfx_buf
, afi_t afi
, safi_t safi
)
444 struct attr
*newattr
, *existattr
;
445 bgp_peer_sort_t new_sort
;
446 bgp_peer_sort_t exist_sort
;
452 uint32_t exist_weight
;
453 uint32_t newm
, existm
;
454 struct in_addr new_id
;
455 struct in_addr exist_id
;
458 int internal_as_route
;
461 char new_buf
[PATH_ADDPATH_STR_BUFFER
];
462 char exist_buf
[PATH_ADDPATH_STR_BUFFER
];
464 uint32_t exist_mm_seq
;
472 zlog_debug("%s: new is NULL", pfx_buf
);
477 bgp_path_info_path_with_addpath_rx_str(new, new_buf
);
481 zlog_debug("%s: %s is the initial bestpath", pfx_buf
,
487 bgp_path_info_path_with_addpath_rx_str(exist
, exist_buf
);
488 zlog_debug("%s: Comparing %s flags 0x%x with %s flags 0x%x",
489 pfx_buf
, new_buf
, new->flags
, exist_buf
,
494 existattr
= exist
->attr
;
496 /* For EVPN routes, we cannot just go by local vs remote, we have to
497 * look at the MAC mobility sequence number, if present.
499 if (safi
== SAFI_EVPN
) {
500 /* This is an error condition described in RFC 7432 Section
502 * states that in this scenario "the PE MUST alert the operator"
504 * does not state what other action to take. In order to provide
506 * consistency in this scenario we are going to prefer the path
510 if (newattr
->sticky
!= existattr
->sticky
) {
512 prefix2str(&new->net
->p
, pfx_buf
,
514 * PREFIX2STR_BUFFER
);
515 bgp_path_info_path_with_addpath_rx_str(new,
517 bgp_path_info_path_with_addpath_rx_str(
521 if (newattr
->sticky
&& !existattr
->sticky
) {
524 "%s: %s wins over %s due to sticky MAC flag",
525 pfx_buf
, new_buf
, exist_buf
);
529 if (!newattr
->sticky
&& existattr
->sticky
) {
532 "%s: %s loses to %s due to sticky MAC flag",
533 pfx_buf
, new_buf
, exist_buf
);
538 new_mm_seq
= mac_mobility_seqnum(newattr
);
539 exist_mm_seq
= mac_mobility_seqnum(existattr
);
541 if (new_mm_seq
> exist_mm_seq
) {
544 "%s: %s wins over %s due to MM seq %u > %u",
545 pfx_buf
, new_buf
, exist_buf
, new_mm_seq
,
550 if (new_mm_seq
< exist_mm_seq
) {
553 "%s: %s loses to %s due to MM seq %u < %u",
554 pfx_buf
, new_buf
, exist_buf
, new_mm_seq
,
560 * if sequence numbers are the same path with the lowest IP
563 nh_cmp
= bgp_path_info_nexthop_cmp(new, exist
);
567 "%s: %s wins over %s due to same MM seq %u and lower IP %s",
568 pfx_buf
, new_buf
, exist_buf
, new_mm_seq
,
569 inet_ntoa(new->attr
->nexthop
));
575 "%s: %s loses to %s due to same MM seq %u and higher IP %s",
576 pfx_buf
, new_buf
, exist_buf
, new_mm_seq
,
577 inet_ntoa(new->attr
->nexthop
));
582 /* 1. Weight check. */
583 new_weight
= newattr
->weight
;
584 exist_weight
= existattr
->weight
;
586 if (new_weight
> exist_weight
) {
588 zlog_debug("%s: %s wins over %s due to weight %d > %d",
589 pfx_buf
, new_buf
, exist_buf
, new_weight
,
594 if (new_weight
< exist_weight
) {
596 zlog_debug("%s: %s loses to %s due to weight %d < %d",
597 pfx_buf
, new_buf
, exist_buf
, new_weight
,
602 /* 2. Local preference check. */
603 new_pref
= exist_pref
= bgp
->default_local_pref
;
605 if (newattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))
606 new_pref
= newattr
->local_pref
;
607 if (existattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))
608 exist_pref
= existattr
->local_pref
;
610 if (new_pref
> exist_pref
) {
613 "%s: %s wins over %s due to localpref %d > %d",
614 pfx_buf
, new_buf
, exist_buf
, new_pref
,
619 if (new_pref
< exist_pref
) {
622 "%s: %s loses to %s due to localpref %d < %d",
623 pfx_buf
, new_buf
, exist_buf
, new_pref
,
628 /* 3. Local route check. We prefer:
630 * - BGP_ROUTE_AGGREGATE
631 * - BGP_ROUTE_REDISTRIBUTE
633 if (!(new->sub_type
== BGP_ROUTE_NORMAL
||
634 new->sub_type
== BGP_ROUTE_IMPORTED
)) {
637 "%s: %s wins over %s due to preferred BGP_ROUTE type",
638 pfx_buf
, new_buf
, exist_buf
);
642 if (!(exist
->sub_type
== BGP_ROUTE_NORMAL
||
643 exist
->sub_type
== BGP_ROUTE_IMPORTED
)) {
646 "%s: %s loses to %s due to preferred BGP_ROUTE type",
647 pfx_buf
, new_buf
, exist_buf
);
651 /* 4. AS path length check. */
652 if (!bgp_flag_check(bgp
, BGP_FLAG_ASPATH_IGNORE
)) {
653 int exist_hops
= aspath_count_hops(existattr
->aspath
);
654 int exist_confeds
= aspath_count_confeds(existattr
->aspath
);
656 if (bgp_flag_check(bgp
, BGP_FLAG_ASPATH_CONFED
)) {
659 aspath_hops
= aspath_count_hops(newattr
->aspath
);
660 aspath_hops
+= aspath_count_confeds(newattr
->aspath
);
662 if (aspath_hops
< (exist_hops
+ exist_confeds
)) {
665 "%s: %s wins over %s due to aspath (with confeds) hopcount %d < %d",
666 pfx_buf
, new_buf
, exist_buf
,
668 (exist_hops
+ exist_confeds
));
672 if (aspath_hops
> (exist_hops
+ exist_confeds
)) {
675 "%s: %s loses to %s due to aspath (with confeds) hopcount %d > %d",
676 pfx_buf
, new_buf
, exist_buf
,
678 (exist_hops
+ exist_confeds
));
682 int newhops
= aspath_count_hops(newattr
->aspath
);
684 if (newhops
< exist_hops
) {
687 "%s: %s wins over %s due to aspath hopcount %d < %d",
688 pfx_buf
, new_buf
, exist_buf
,
689 newhops
, exist_hops
);
693 if (newhops
> exist_hops
) {
696 "%s: %s loses to %s due to aspath hopcount %d > %d",
697 pfx_buf
, new_buf
, exist_buf
,
698 newhops
, exist_hops
);
704 /* 5. Origin check. */
705 if (newattr
->origin
< existattr
->origin
) {
707 zlog_debug("%s: %s wins over %s due to ORIGIN %s < %s",
708 pfx_buf
, new_buf
, exist_buf
,
709 bgp_origin_long_str
[newattr
->origin
],
710 bgp_origin_long_str
[existattr
->origin
]);
714 if (newattr
->origin
> existattr
->origin
) {
716 zlog_debug("%s: %s loses to %s due to ORIGIN %s > %s",
717 pfx_buf
, new_buf
, exist_buf
,
718 bgp_origin_long_str
[newattr
->origin
],
719 bgp_origin_long_str
[existattr
->origin
]);
724 internal_as_route
= (aspath_count_hops(newattr
->aspath
) == 0
725 && aspath_count_hops(existattr
->aspath
) == 0);
726 confed_as_route
= (aspath_count_confeds(newattr
->aspath
) > 0
727 && aspath_count_confeds(existattr
->aspath
) > 0
728 && aspath_count_hops(newattr
->aspath
) == 0
729 && aspath_count_hops(existattr
->aspath
) == 0);
731 if (bgp_flag_check(bgp
, BGP_FLAG_ALWAYS_COMPARE_MED
)
732 || (bgp_flag_check(bgp
, BGP_FLAG_MED_CONFED
) && confed_as_route
)
733 || aspath_cmp_left(newattr
->aspath
, existattr
->aspath
)
734 || aspath_cmp_left_confed(newattr
->aspath
, existattr
->aspath
)
735 || internal_as_route
) {
736 new_med
= bgp_med_value(new->attr
, bgp
);
737 exist_med
= bgp_med_value(exist
->attr
, bgp
);
739 if (new_med
< exist_med
) {
742 "%s: %s wins over %s due to MED %d < %d",
743 pfx_buf
, new_buf
, exist_buf
, new_med
,
748 if (new_med
> exist_med
) {
751 "%s: %s loses to %s due to MED %d > %d",
752 pfx_buf
, new_buf
, exist_buf
, new_med
,
758 /* 7. Peer type check. */
759 new_sort
= new->peer
->sort
;
760 exist_sort
= exist
->peer
->sort
;
762 if (new_sort
== BGP_PEER_EBGP
763 && (exist_sort
== BGP_PEER_IBGP
|| exist_sort
== BGP_PEER_CONFED
)) {
766 "%s: %s wins over %s due to eBGP peer > iBGP peer",
767 pfx_buf
, new_buf
, exist_buf
);
771 if (exist_sort
== BGP_PEER_EBGP
772 && (new_sort
== BGP_PEER_IBGP
|| new_sort
== BGP_PEER_CONFED
)) {
775 "%s: %s loses to %s due to iBGP peer < eBGP peer",
776 pfx_buf
, new_buf
, exist_buf
);
780 /* 8. IGP metric check. */
784 newm
= new->extra
->igpmetric
;
786 existm
= exist
->extra
->igpmetric
;
791 "%s: %s wins over %s due to IGP metric %d < %d",
792 pfx_buf
, new_buf
, exist_buf
, newm
, existm
);
799 "%s: %s loses to %s due to IGP metric %d > %d",
800 pfx_buf
, new_buf
, exist_buf
, newm
, existm
);
804 /* 9. Same IGP metric. Compare the cluster list length as
805 representative of IGP hops metric. Rewrite the metric value
806 pair (newm, existm) with the cluster list length. Prefer the
807 path with smaller cluster list length. */
808 if (newm
== existm
) {
809 if (peer_sort(new->peer
) == BGP_PEER_IBGP
810 && peer_sort(exist
->peer
) == BGP_PEER_IBGP
811 && (mpath_cfg
== NULL
813 mpath_cfg
->ibgp_flags
,
814 BGP_FLAG_IBGP_MULTIPATH_SAME_CLUSTERLEN
))) {
815 newm
= BGP_CLUSTER_LIST_LENGTH(new->attr
);
816 existm
= BGP_CLUSTER_LIST_LENGTH(exist
->attr
);
821 "%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
822 pfx_buf
, new_buf
, exist_buf
,
830 "%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
831 pfx_buf
, new_buf
, exist_buf
,
838 /* 10. confed-external vs. confed-internal */
839 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
840 if (new_sort
== BGP_PEER_CONFED
841 && exist_sort
== BGP_PEER_IBGP
) {
844 "%s: %s wins over %s due to confed-external peer > confed-internal peer",
845 pfx_buf
, new_buf
, exist_buf
);
849 if (exist_sort
== BGP_PEER_CONFED
850 && new_sort
== BGP_PEER_IBGP
) {
853 "%s: %s loses to %s due to confed-internal peer < confed-external peer",
854 pfx_buf
, new_buf
, exist_buf
);
859 /* 11. Maximum path check. */
860 if (newm
== existm
) {
861 /* If one path has a label but the other does not, do not treat
862 * them as equals for multipath
864 if ((new->extra
&&bgp_is_valid_label(&new->extra
->label
[0]))
866 && bgp_is_valid_label(&exist
->extra
->label
[0]))) {
869 "%s: %s and %s cannot be multipath, one has a label while the other does not",
870 pfx_buf
, new_buf
, exist_buf
);
871 } else if (bgp_flag_check(bgp
,
872 BGP_FLAG_ASPATH_MULTIPATH_RELAX
)) {
875 * For the two paths, all comparison steps till IGP
877 * have succeeded - including AS_PATH hop count. Since
879 * bestpath as-path multipath-relax' knob is on, we
881 * an exact match of AS_PATH. Thus, mark the paths are
883 * That will trigger both these paths to get into the
891 "%s: %s and %s are equal via multipath-relax",
892 pfx_buf
, new_buf
, exist_buf
);
893 } else if (new->peer
->sort
== BGP_PEER_IBGP
) {
894 if (aspath_cmp(new->attr
->aspath
,
895 exist
->attr
->aspath
)) {
900 "%s: %s and %s are equal via matching aspaths",
901 pfx_buf
, new_buf
, exist_buf
);
903 } else if (new->peer
->as
== exist
->peer
->as
) {
908 "%s: %s and %s are equal via same remote-as",
909 pfx_buf
, new_buf
, exist_buf
);
913 * TODO: If unequal cost ibgp multipath is enabled we can
914 * mark the paths as equal here instead of returning
919 "%s: %s wins over %s after IGP metric comparison",
920 pfx_buf
, new_buf
, exist_buf
);
923 "%s: %s loses to %s after IGP metric comparison",
924 pfx_buf
, new_buf
, exist_buf
);
929 /* 12. If both paths are external, prefer the path that was received
930 first (the oldest one). This step minimizes route-flap, since a
931 newer path won't displace an older one, even if it was the
932 preferred route based on the additional decision criteria below. */
933 if (!bgp_flag_check(bgp
, BGP_FLAG_COMPARE_ROUTER_ID
)
934 && new_sort
== BGP_PEER_EBGP
&& exist_sort
== BGP_PEER_EBGP
) {
935 if (CHECK_FLAG(new->flags
, BGP_PATH_SELECTED
)) {
938 "%s: %s wins over %s due to oldest external",
939 pfx_buf
, new_buf
, exist_buf
);
943 if (CHECK_FLAG(exist
->flags
, BGP_PATH_SELECTED
)) {
946 "%s: %s loses to %s due to oldest external",
947 pfx_buf
, new_buf
, exist_buf
);
952 /* 13. Router-ID comparision. */
953 /* If one of the paths is "stale", the corresponding peer router-id will
954 * be 0 and would always win over the other path. If originator id is
955 * used for the comparision, it will decide which path is better.
957 if (newattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
958 new_id
.s_addr
= newattr
->originator_id
.s_addr
;
960 new_id
.s_addr
= new->peer
->remote_id
.s_addr
;
961 if (existattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
962 exist_id
.s_addr
= existattr
->originator_id
.s_addr
;
964 exist_id
.s_addr
= exist
->peer
->remote_id
.s_addr
;
966 if (ntohl(new_id
.s_addr
) < ntohl(exist_id
.s_addr
)) {
969 "%s: %s wins over %s due to Router-ID comparison",
970 pfx_buf
, new_buf
, exist_buf
);
974 if (ntohl(new_id
.s_addr
) > ntohl(exist_id
.s_addr
)) {
977 "%s: %s loses to %s due to Router-ID comparison",
978 pfx_buf
, new_buf
, exist_buf
);
982 /* 14. Cluster length comparision. */
983 new_cluster
= BGP_CLUSTER_LIST_LENGTH(new->attr
);
984 exist_cluster
= BGP_CLUSTER_LIST_LENGTH(exist
->attr
);
986 if (new_cluster
< exist_cluster
) {
989 "%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
990 pfx_buf
, new_buf
, exist_buf
, new_cluster
,
995 if (new_cluster
> exist_cluster
) {
998 "%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
999 pfx_buf
, new_buf
, exist_buf
, new_cluster
,
1004 /* 15. Neighbor address comparision. */
1005 /* Do this only if neither path is "stale" as stale paths do not have
1006 * valid peer information (as the connection may or may not be up).
1008 if (CHECK_FLAG(exist
->flags
, BGP_PATH_STALE
)) {
1011 "%s: %s wins over %s due to latter path being STALE",
1012 pfx_buf
, new_buf
, exist_buf
);
1016 if (CHECK_FLAG(new->flags
, BGP_PATH_STALE
)) {
1019 "%s: %s loses to %s due to former path being STALE",
1020 pfx_buf
, new_buf
, exist_buf
);
1024 /* locally configured routes to advertise do not have su_remote */
1025 if (new->peer
->su_remote
== NULL
)
1027 if (exist
->peer
->su_remote
== NULL
)
1030 ret
= sockunion_cmp(new->peer
->su_remote
, exist
->peer
->su_remote
);
1035 "%s: %s loses to %s due to Neighor IP comparison",
1036 pfx_buf
, new_buf
, exist_buf
);
1043 "%s: %s wins over %s due to Neighor IP comparison",
1044 pfx_buf
, new_buf
, exist_buf
);
1049 zlog_debug("%s: %s wins over %s due to nothing left to compare",
1050 pfx_buf
, new_buf
, exist_buf
);
1055 /* Compare two bgp route entity. Return -1 if new is preferred, 1 if exist
1056 * is preferred, or 0 if they are the same (usually will only occur if
1057 * multipath is enabled
1058 * This version is compatible with */
1059 int bgp_path_info_cmp_compatible(struct bgp
*bgp
, struct bgp_path_info
*new,
1060 struct bgp_path_info
*exist
, char *pfx_buf
,
1061 afi_t afi
, safi_t safi
)
1065 ret
= bgp_path_info_cmp(bgp
, new, exist
, &paths_eq
, NULL
, 0, pfx_buf
,
1079 static enum filter_type
bgp_input_filter(struct peer
*peer
, struct prefix
*p
,
1080 struct attr
*attr
, afi_t afi
,
1083 struct bgp_filter
*filter
;
1085 filter
= &peer
->filter
[afi
][safi
];
1087 #define FILTER_EXIST_WARN(F, f, filter) \
1088 if (BGP_DEBUG(update, UPDATE_IN) && !(F##_IN(filter))) \
1089 zlog_debug("%s: Could not find configured input %s-list %s!", \
1090 peer->host, #f, F##_IN_NAME(filter));
1092 if (DISTRIBUTE_IN_NAME(filter
)) {
1093 FILTER_EXIST_WARN(DISTRIBUTE
, distribute
, filter
);
1095 if (access_list_apply(DISTRIBUTE_IN(filter
), p
) == FILTER_DENY
)
1099 if (PREFIX_LIST_IN_NAME(filter
)) {
1100 FILTER_EXIST_WARN(PREFIX_LIST
, prefix
, filter
);
1102 if (prefix_list_apply(PREFIX_LIST_IN(filter
), p
) == PREFIX_DENY
)
1106 if (FILTER_LIST_IN_NAME(filter
)) {
1107 FILTER_EXIST_WARN(FILTER_LIST
, as
, filter
);
1109 if (as_list_apply(FILTER_LIST_IN(filter
), attr
->aspath
)
1114 return FILTER_PERMIT
;
1115 #undef FILTER_EXIST_WARN
1118 static enum filter_type
bgp_output_filter(struct peer
*peer
, struct prefix
*p
,
1119 struct attr
*attr
, afi_t afi
,
1122 struct bgp_filter
*filter
;
1124 filter
= &peer
->filter
[afi
][safi
];
1126 #define FILTER_EXIST_WARN(F, f, filter) \
1127 if (BGP_DEBUG(update, UPDATE_OUT) && !(F##_OUT(filter))) \
1128 zlog_debug("%s: Could not find configured output %s-list %s!", \
1129 peer->host, #f, F##_OUT_NAME(filter));
1131 if (DISTRIBUTE_OUT_NAME(filter
)) {
1132 FILTER_EXIST_WARN(DISTRIBUTE
, distribute
, filter
);
1134 if (access_list_apply(DISTRIBUTE_OUT(filter
), p
) == FILTER_DENY
)
1138 if (PREFIX_LIST_OUT_NAME(filter
)) {
1139 FILTER_EXIST_WARN(PREFIX_LIST
, prefix
, filter
);
1141 if (prefix_list_apply(PREFIX_LIST_OUT(filter
), p
)
1146 if (FILTER_LIST_OUT_NAME(filter
)) {
1147 FILTER_EXIST_WARN(FILTER_LIST
, as
, filter
);
1149 if (as_list_apply(FILTER_LIST_OUT(filter
), attr
->aspath
)
1154 return FILTER_PERMIT
;
1155 #undef FILTER_EXIST_WARN
1158 /* If community attribute includes no_export then return 1. */
1159 static int bgp_community_filter(struct peer
*peer
, struct attr
*attr
)
1161 if (attr
->community
) {
1162 /* NO_ADVERTISE check. */
1163 if (community_include(attr
->community
, COMMUNITY_NO_ADVERTISE
))
1166 /* NO_EXPORT check. */
1167 if (peer
->sort
== BGP_PEER_EBGP
1168 && community_include(attr
->community
, COMMUNITY_NO_EXPORT
))
1171 /* NO_EXPORT_SUBCONFED check. */
1172 if (peer
->sort
== BGP_PEER_EBGP
1173 || peer
->sort
== BGP_PEER_CONFED
)
1174 if (community_include(attr
->community
,
1175 COMMUNITY_NO_EXPORT_SUBCONFED
))
1181 /* Route reflection loop check. */
1182 static int bgp_cluster_filter(struct peer
*peer
, struct attr
*attr
)
1184 struct in_addr cluster_id
;
1186 if (attr
->cluster
) {
1187 if (peer
->bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
1188 cluster_id
= peer
->bgp
->cluster_id
;
1190 cluster_id
= peer
->bgp
->router_id
;
1192 if (cluster_loop_check(attr
->cluster
, cluster_id
))
1198 static int bgp_input_modifier(struct peer
*peer
, struct prefix
*p
,
1199 struct attr
*attr
, afi_t afi
, safi_t safi
,
1200 const char *rmap_name
)
1202 struct bgp_filter
*filter
;
1203 struct bgp_path_info rmap_path
;
1204 route_map_result_t ret
;
1205 struct route_map
*rmap
= NULL
;
1207 filter
= &peer
->filter
[afi
][safi
];
1209 /* Apply default weight value. */
1210 if (peer
->weight
[afi
][safi
])
1211 attr
->weight
= peer
->weight
[afi
][safi
];
1214 rmap
= route_map_lookup_by_name(rmap_name
);
1219 if (ROUTE_MAP_IN_NAME(filter
)) {
1220 rmap
= ROUTE_MAP_IN(filter
);
1227 /* Route map apply. */
1229 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
1230 /* Duplicate current value to new strucutre for modification. */
1231 rmap_path
.peer
= peer
;
1232 rmap_path
.attr
= attr
;
1234 SET_FLAG(peer
->rmap_type
, PEER_RMAP_TYPE_IN
);
1236 /* Apply BGP route map to the attribute. */
1237 ret
= route_map_apply(rmap
, p
, RMAP_BGP
, &rmap_path
);
1239 peer
->rmap_type
= 0;
1241 if (ret
== RMAP_DENYMATCH
)
1247 static int bgp_output_modifier(struct peer
*peer
, struct prefix
*p
,
1248 struct attr
*attr
, afi_t afi
, safi_t safi
,
1249 const char *rmap_name
)
1251 struct bgp_path_info rmap_path
;
1252 route_map_result_t ret
;
1253 struct route_map
*rmap
= NULL
;
1257 * So if we get to this point and have no rmap_name
1258 * we want to just show the output as it currently
1264 /* Apply default weight value. */
1265 if (peer
->weight
[afi
][safi
])
1266 attr
->weight
= peer
->weight
[afi
][safi
];
1268 rmap
= route_map_lookup_by_name(rmap_name
);
1271 * If we have a route map name and we do not find
1272 * the routemap that means we have an implicit
1278 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
1279 /* Route map apply. */
1280 /* Duplicate current value to new strucutre for modification. */
1281 rmap_path
.peer
= peer
;
1282 rmap_path
.attr
= attr
;
1284 rmap_type
= peer
->rmap_type
;
1285 SET_FLAG(peer
->rmap_type
, PEER_RMAP_TYPE_OUT
);
1287 /* Apply BGP route map to the attribute. */
1288 ret
= route_map_apply(rmap
, p
, RMAP_BGP
, &rmap_path
);
1290 peer
->rmap_type
= rmap_type
;
1292 if (ret
== RMAP_DENYMATCH
)
1294 * caller has multiple error paths with bgp_attr_flush()
1301 /* If this is an EBGP peer with remove-private-AS */
1302 static void bgp_peer_remove_private_as(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1303 struct peer
*peer
, struct attr
*attr
)
1305 if (peer
->sort
== BGP_PEER_EBGP
1306 && (peer_af_flag_check(peer
, afi
, safi
,
1307 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
)
1308 || peer_af_flag_check(peer
, afi
, safi
,
1309 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
)
1310 || peer_af_flag_check(peer
, afi
, safi
,
1311 PEER_FLAG_REMOVE_PRIVATE_AS_ALL
)
1312 || peer_af_flag_check(peer
, afi
, safi
,
1313 PEER_FLAG_REMOVE_PRIVATE_AS
))) {
1314 // Take action on the entire aspath
1315 if (peer_af_flag_check(peer
, afi
, safi
,
1316 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
)
1317 || peer_af_flag_check(peer
, afi
, safi
,
1318 PEER_FLAG_REMOVE_PRIVATE_AS_ALL
)) {
1319 if (peer_af_flag_check(
1321 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
))
1322 attr
->aspath
= aspath_replace_private_asns(
1323 attr
->aspath
, bgp
->as
);
1325 // The entire aspath consists of private ASNs so create
1327 else if (aspath_private_as_check(attr
->aspath
))
1328 attr
->aspath
= aspath_empty_get();
1330 // There are some public and some private ASNs, remove
1333 attr
->aspath
= aspath_remove_private_asns(
1337 // 'all' was not specified so the entire aspath must be private
1339 // for us to do anything
1340 else if (aspath_private_as_check(attr
->aspath
)) {
1341 if (peer_af_flag_check(
1343 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
))
1344 attr
->aspath
= aspath_replace_private_asns(
1345 attr
->aspath
, bgp
->as
);
1347 attr
->aspath
= aspath_empty_get();
1352 /* If this is an EBGP peer with as-override */
1353 static void bgp_peer_as_override(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1354 struct peer
*peer
, struct attr
*attr
)
1356 if (peer
->sort
== BGP_PEER_EBGP
1357 && peer_af_flag_check(peer
, afi
, safi
, PEER_FLAG_AS_OVERRIDE
)) {
1358 if (aspath_single_asn_check(attr
->aspath
, peer
->as
))
1359 attr
->aspath
= aspath_replace_specific_asn(
1360 attr
->aspath
, peer
->as
, bgp
->as
);
1364 void bgp_attr_add_gshut_community(struct attr
*attr
)
1366 struct community
*old
;
1367 struct community
*new;
1368 struct community
*merge
;
1369 struct community
*gshut
;
1371 old
= attr
->community
;
1372 gshut
= community_str2com("graceful-shutdown");
1377 merge
= community_merge(community_dup(old
), gshut
);
1379 if (old
->refcnt
== 0)
1380 community_free(&old
);
1382 new = community_uniq_sort(merge
);
1383 community_free(&merge
);
1385 new = community_dup(gshut
);
1388 community_free(&gshut
);
1389 attr
->community
= new;
1390 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
1392 /* When we add the graceful-shutdown community we must also
1393 * lower the local-preference */
1394 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1395 attr
->local_pref
= BGP_GSHUT_LOCAL_PREF
;
1399 static void subgroup_announce_reset_nhop(uint8_t family
, struct attr
*attr
)
1401 if (family
== AF_INET
) {
1402 attr
->nexthop
.s_addr
= 0;
1403 attr
->mp_nexthop_global_in
.s_addr
= 0;
1405 if (family
== AF_INET6
)
1406 memset(&attr
->mp_nexthop_global
, 0, IPV6_MAX_BYTELEN
);
1407 if (family
== AF_EVPN
)
1408 memset(&attr
->mp_nexthop_global_in
, 0, BGP_ATTR_NHLEN_IPV4
);
1411 int subgroup_announce_check(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
1412 struct update_subgroup
*subgrp
, struct prefix
*p
,
1415 struct bgp_filter
*filter
;
1418 struct peer
*onlypeer
;
1420 struct attr
*piattr
;
1421 char buf
[PREFIX_STRLEN
];
1427 int samepeer_safe
= 0; /* for synthetic mplsvpns routes */
1429 if (DISABLE_BGP_ANNOUNCE
)
1432 afi
= SUBGRP_AFI(subgrp
);
1433 safi
= SUBGRP_SAFI(subgrp
);
1434 peer
= SUBGRP_PEER(subgrp
);
1436 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
))
1437 onlypeer
= SUBGRP_PFIRST(subgrp
)->peer
;
1440 filter
= &peer
->filter
[afi
][safi
];
1441 bgp
= SUBGRP_INST(subgrp
);
1442 piattr
= bgp_path_info_mpath_count(pi
) ? bgp_path_info_mpath_attr(pi
)
1446 if (((afi
== AFI_IP
) || (afi
== AFI_IP6
)) && (safi
== SAFI_MPLS_VPN
)
1447 && ((pi
->type
== ZEBRA_ROUTE_BGP_DIRECT
)
1448 || (pi
->type
== ZEBRA_ROUTE_BGP_DIRECT_EXT
))) {
1451 * direct and direct_ext type routes originate internally even
1452 * though they can have peer pointers that reference other
1455 prefix2str(p
, buf
, PREFIX_STRLEN
);
1456 zlog_debug("%s: pfx %s bgp_direct->vpn route peer safe",
1462 if (((afi
== AFI_IP
) || (afi
== AFI_IP6
))
1463 && ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_UNICAST
))
1464 && (pi
->type
== ZEBRA_ROUTE_BGP
)
1465 && (pi
->sub_type
== BGP_ROUTE_IMPORTED
)) {
1467 /* Applies to routes leaked vpn->vrf and vrf->vpn */
1472 /* With addpath we may be asked to TX all kinds of paths so make sure
1474 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
1475 || CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
)
1476 || CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1480 /* If this is not the bestpath then check to see if there is an enabled
1482 * feature that requires us to advertise it */
1483 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1484 if (!bgp_addpath_tx_path(peer
->addpath_type
[afi
][safi
], pi
)) {
1489 /* Aggregate-address suppress check. */
1490 if (pi
->extra
&& pi
->extra
->suppress
)
1491 if (!UNSUPPRESS_MAP_NAME(filter
)) {
1496 * If we are doing VRF 2 VRF leaking via the import
1497 * statement, we want to prevent the route going
1498 * off box as that the RT and RD created are localy
1499 * significant and globaly useless.
1501 if (safi
== SAFI_MPLS_VPN
&& pi
->extra
&& pi
->extra
->num_labels
1502 && pi
->extra
->label
[0] == BGP_PREVENT_VRF_2_VRF_LEAK
)
1505 /* If it's labeled safi, make sure the route has a valid label. */
1506 if (safi
== SAFI_LABELED_UNICAST
) {
1507 mpls_label_t label
= bgp_adv_label(rn
, pi
, peer
, afi
, safi
);
1508 if (!bgp_is_valid_label(&label
)) {
1509 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1510 zlog_debug("u%" PRIu64
":s%" PRIu64
1511 " %s/%d is filtered - no label (%p)",
1512 subgrp
->update_group
->id
, subgrp
->id
,
1513 inet_ntop(p
->family
, &p
->u
.prefix
,
1514 buf
, SU_ADDRSTRLEN
),
1515 p
->prefixlen
, &label
);
1520 /* Do not send back route to sender. */
1521 if (onlypeer
&& from
== onlypeer
) {
1525 /* Do not send the default route in the BGP table if the neighbor is
1526 * configured for default-originate */
1527 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1528 PEER_FLAG_DEFAULT_ORIGINATE
)) {
1529 if (p
->family
== AF_INET
&& p
->u
.prefix4
.s_addr
== INADDR_ANY
)
1531 else if (p
->family
== AF_INET6
&& p
->prefixlen
== 0)
1535 /* Transparency check. */
1536 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_RSERVER_CLIENT
)
1537 && CHECK_FLAG(from
->af_flags
[afi
][safi
], PEER_FLAG_RSERVER_CLIENT
))
1542 /* If community is not disabled check the no-export and local. */
1543 if (!transparent
&& bgp_community_filter(peer
, piattr
)) {
1544 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1546 "subgrpannouncecheck: community filter check fail");
1550 /* If the attribute has originator-id and it is same as remote
1552 if (onlypeer
&& piattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
)
1553 && (IPV4_ADDR_SAME(&onlypeer
->remote_id
, &piattr
->originator_id
))) {
1554 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1556 "%s [Update:SEND] %s originator-id is same as "
1559 prefix2str(p
, buf
, sizeof(buf
)));
1563 /* ORF prefix-list filter check */
1564 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_ADV
)
1565 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
1566 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1567 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
)))
1568 if (peer
->orf_plist
[afi
][safi
]) {
1569 if (prefix_list_apply(peer
->orf_plist
[afi
][safi
], p
)
1571 if (bgp_debug_update(NULL
, p
,
1572 subgrp
->update_group
, 0))
1574 "%s [Update:SEND] %s is filtered via ORF",
1582 /* Output filter check. */
1583 if (bgp_output_filter(peer
, p
, piattr
, afi
, safi
) == FILTER_DENY
) {
1584 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1585 zlog_debug("%s [Update:SEND] %s is filtered",
1586 peer
->host
, prefix2str(p
, buf
, sizeof(buf
)));
1590 #ifdef BGP_SEND_ASPATH_CHECK
1591 /* AS path loop check. */
1592 if (onlypeer
&& aspath_loop_check(piattr
->aspath
, onlypeer
->as
)) {
1593 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1595 "%s [Update:SEND] suppress announcement to peer AS %u "
1596 "that is part of AS path.",
1597 onlypeer
->host
, onlypeer
->as
);
1600 #endif /* BGP_SEND_ASPATH_CHECK */
1602 /* If we're a CONFED we need to loop check the CONFED ID too */
1603 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1604 if (aspath_loop_check(piattr
->aspath
, bgp
->confed_id
)) {
1605 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1607 "%s [Update:SEND] suppress announcement to peer AS %u"
1609 peer
->host
, bgp
->confed_id
);
1614 /* Route-Reflect check. */
1615 if (from
->sort
== BGP_PEER_IBGP
&& peer
->sort
== BGP_PEER_IBGP
)
1620 /* IBGP reflection check. */
1621 if (reflect
&& !samepeer_safe
) {
1622 /* A route from a Client peer. */
1623 if (CHECK_FLAG(from
->af_flags
[afi
][safi
],
1624 PEER_FLAG_REFLECTOR_CLIENT
)) {
1625 /* Reflect to all the Non-Client peers and also to the
1626 Client peers other than the originator. Originator
1628 is already done. So there is noting to do. */
1629 /* no bgp client-to-client reflection check. */
1630 if (bgp_flag_check(bgp
, BGP_FLAG_NO_CLIENT_TO_CLIENT
))
1631 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1632 PEER_FLAG_REFLECTOR_CLIENT
))
1635 /* A route from a Non-client peer. Reflect to all other
1637 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1638 PEER_FLAG_REFLECTOR_CLIENT
))
1643 /* For modify attribute, copy it to temporary structure. */
1644 bgp_attr_dup(attr
, piattr
);
1646 /* If local-preference is not set. */
1647 if ((peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
)
1648 && (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)))) {
1649 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1650 attr
->local_pref
= bgp
->default_local_pref
;
1653 /* If originator-id is not set and the route is to be reflected,
1654 set the originator id */
1656 && (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
)))) {
1657 IPV4_ADDR_COPY(&(attr
->originator_id
), &(from
->remote_id
));
1658 SET_FLAG(attr
->flag
, BGP_ATTR_ORIGINATOR_ID
);
1661 /* Remove MED if its an EBGP peer - will get overwritten by route-maps
1663 if (peer
->sort
== BGP_PEER_EBGP
1664 && attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
1665 if (from
!= bgp
->peer_self
&& !transparent
1666 && !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1667 PEER_FLAG_MED_UNCHANGED
))
1669 ~(ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
));
1672 /* Since the nexthop attribute can vary per peer, it is not explicitly
1674 * in announce check, only certain flags and length (or number of
1676 * -- for IPv6/MP_REACH) are set here in order to guide the update
1678 * code in setting the nexthop(s) on a per peer basis in
1680 * Typically, the source nexthop in the attribute is preserved but in
1682 * scenarios where we know it will always be overwritten, we reset the
1683 * nexthop to "0" in an attempt to achieve better Update packing. An
1684 * example of this is when a prefix from each of 2 IBGP peers needs to
1686 * announced to an EBGP peer (and they have the same attributes barring
1690 SET_FLAG(attr
->rmap_change_flags
, BATTR_REFLECTED
);
1692 #define NEXTHOP_IS_V6 \
1693 ((safi != SAFI_ENCAP && safi != SAFI_MPLS_VPN \
1694 && (p->family == AF_INET6 || peer_cap_enhe(peer, afi, safi))) \
1695 || ((safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN) \
1696 && attr->mp_nexthop_len >= IPV6_MAX_BYTELEN))
1698 /* IPv6/MP starts with 1 nexthop. The link-local address is passed only
1700 * the peer (group) is configured to receive link-local nexthop
1702 * and it is available in the prefix OR we're not reflecting the route
1704 * the peer (group) to whom we're going to announce is on a shared
1706 * and this is either a self-originated route or the peer is EBGP.
1708 if (NEXTHOP_IS_V6
) {
1709 attr
->mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL
;
1710 if ((CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1711 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
)
1712 && IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_local
))
1713 || (!reflect
&& peer
->shared_network
1714 && (from
== bgp
->peer_self
1715 || peer
->sort
== BGP_PEER_EBGP
))) {
1716 attr
->mp_nexthop_len
=
1717 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
;
1720 /* Clear off link-local nexthop in source, whenever it is not
1722 * ensure more prefixes share the same attribute for
1725 if (!(CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1726 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
)))
1727 memset(&attr
->mp_nexthop_local
, 0, IPV6_MAX_BYTELEN
);
1730 bgp_peer_remove_private_as(bgp
, afi
, safi
, peer
, attr
);
1731 bgp_peer_as_override(bgp
, afi
, safi
, peer
, attr
);
1733 /* Route map & unsuppress-map apply. */
1734 if (ROUTE_MAP_OUT_NAME(filter
) || (pi
->extra
&& pi
->extra
->suppress
)) {
1735 struct bgp_path_info rmap_path
;
1736 struct bgp_path_info_extra dummy_rmap_path_extra
;
1737 struct attr dummy_attr
;
1739 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
1740 rmap_path
.peer
= peer
;
1741 rmap_path
.attr
= attr
;
1744 memcpy(&dummy_rmap_path_extra
, pi
->extra
,
1745 sizeof(struct bgp_path_info_extra
));
1746 rmap_path
.extra
= &dummy_rmap_path_extra
;
1749 /* don't confuse inbound and outbound setting */
1750 RESET_FLAG(attr
->rmap_change_flags
);
1753 * The route reflector is not allowed to modify the attributes
1754 * of the reflected IBGP routes unless explicitly allowed.
1756 if ((from
->sort
== BGP_PEER_IBGP
&& peer
->sort
== BGP_PEER_IBGP
)
1757 && !bgp_flag_check(bgp
,
1758 BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY
)) {
1759 bgp_attr_dup(&dummy_attr
, attr
);
1760 rmap_path
.attr
= &dummy_attr
;
1763 SET_FLAG(peer
->rmap_type
, PEER_RMAP_TYPE_OUT
);
1765 if (pi
->extra
&& pi
->extra
->suppress
)
1766 ret
= route_map_apply(UNSUPPRESS_MAP(filter
), p
,
1767 RMAP_BGP
, &rmap_path
);
1769 ret
= route_map_apply(ROUTE_MAP_OUT(filter
), p
,
1770 RMAP_BGP
, &rmap_path
);
1772 peer
->rmap_type
= 0;
1774 if (ret
== RMAP_DENYMATCH
) {
1775 bgp_attr_flush(attr
);
1780 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
)) {
1781 if (peer
->sort
== BGP_PEER_IBGP
1782 || peer
->sort
== BGP_PEER_CONFED
) {
1783 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1784 attr
->local_pref
= BGP_GSHUT_LOCAL_PREF
;
1786 bgp_attr_add_gshut_community(attr
);
1790 /* After route-map has been applied, we check to see if the nexthop to
1791 * be carried in the attribute (that is used for the announcement) can
1792 * be cleared off or not. We do this in all cases where we would be
1793 * setting the nexthop to "ourselves". For IPv6, we only need to
1795 * the global nexthop here; the link-local nexthop would have been
1797 * already, and if not, it is required by the update formation code.
1798 * Also see earlier comments in this function.
1801 * If route-map has performed some operation on the nexthop or the peer
1802 * configuration says to pass it unchanged, we cannot reset the nexthop
1803 * here, so only attempt to do it if these aren't true. Note that the
1804 * route-map handler itself might have cleared the nexthop, if for
1806 * it is configured as 'peer-address'.
1808 if (!bgp_rmap_nhop_changed(attr
->rmap_change_flags
,
1809 piattr
->rmap_change_flags
)
1811 && !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1812 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
1813 /* We can reset the nexthop, if setting (or forcing) it to
1815 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1816 PEER_FLAG_NEXTHOP_SELF
)
1817 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1818 PEER_FLAG_FORCE_NEXTHOP_SELF
)) {
1820 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1821 PEER_FLAG_FORCE_NEXTHOP_SELF
))
1822 subgroup_announce_reset_nhop(
1823 (peer_cap_enhe(peer
, afi
, safi
)
1827 } else if (peer
->sort
== BGP_PEER_EBGP
) {
1828 /* Can also reset the nexthop if announcing to EBGP, but
1830 * no peer in the subgroup is on a shared subnet.
1831 * Note: 3rd party nexthop currently implemented for
1834 if (!bgp_subgrp_multiaccess_check_v4(piattr
->nexthop
,
1836 subgroup_announce_reset_nhop(
1837 (peer_cap_enhe(peer
, afi
, safi
)
1841 } else if (CHECK_FLAG(pi
->flags
, BGP_PATH_ANNC_NH_SELF
)) {
1843 * This flag is used for leaked vpn-vrf routes
1845 int family
= p
->family
;
1847 if (peer_cap_enhe(peer
, afi
, safi
))
1850 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1852 "%s: BGP_PATH_ANNC_NH_SELF, family=%s",
1853 __func__
, family2str(family
));
1854 subgroup_announce_reset_nhop(family
, attr
);
1857 /* If IPv6/MP and nexthop does not have any override and happens
1859 * be a link-local address, reset it so that we don't pass along
1861 * source's link-local IPv6 address to recipients who may not be
1863 * the same interface.
1865 if (p
->family
== AF_INET6
|| peer_cap_enhe(peer
, afi
, safi
)) {
1866 if (IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_global
))
1867 subgroup_announce_reset_nhop(AF_INET6
, attr
);
1874 void bgp_best_selection(struct bgp
*bgp
, struct bgp_node
*rn
,
1875 struct bgp_maxpaths_cfg
*mpath_cfg
,
1876 struct bgp_path_info_pair
*result
, afi_t afi
,
1879 struct bgp_path_info
*new_select
;
1880 struct bgp_path_info
*old_select
;
1881 struct bgp_path_info
*pi
;
1882 struct bgp_path_info
*pi1
;
1883 struct bgp_path_info
*pi2
;
1884 struct bgp_path_info
*nextpi
= NULL
;
1885 int paths_eq
, do_mpath
, debug
;
1886 struct list mp_list
;
1887 char pfx_buf
[PREFIX2STR_BUFFER
];
1888 char path_buf
[PATH_ADDPATH_STR_BUFFER
];
1890 bgp_mp_list_init(&mp_list
);
1892 (mpath_cfg
->maxpaths_ebgp
> 1 || mpath_cfg
->maxpaths_ibgp
> 1);
1894 debug
= bgp_debug_bestpath(&rn
->p
);
1897 prefix2str(&rn
->p
, pfx_buf
, sizeof(pfx_buf
));
1899 /* bgp deterministic-med */
1901 if (bgp_flag_check(bgp
, BGP_FLAG_DETERMINISTIC_MED
)) {
1903 /* Clear BGP_PATH_DMED_SELECTED for all paths */
1904 for (pi1
= bgp_node_get_bgp_path_info(rn
); pi1
;
1906 bgp_path_info_unset_flag(rn
, pi1
,
1907 BGP_PATH_DMED_SELECTED
);
1909 for (pi1
= bgp_node_get_bgp_path_info(rn
); pi1
;
1911 if (CHECK_FLAG(pi1
->flags
, BGP_PATH_DMED_CHECK
))
1913 if (BGP_PATH_HOLDDOWN(pi1
))
1915 if (pi1
->peer
&& pi1
->peer
!= bgp
->peer_self
)
1916 if (pi1
->peer
->status
!= Established
)
1921 for (pi2
= pi1
->next
; pi2
; pi2
= pi2
->next
) {
1922 if (CHECK_FLAG(pi2
->flags
,
1923 BGP_PATH_DMED_CHECK
))
1925 if (BGP_PATH_HOLDDOWN(pi2
))
1928 && pi2
->peer
!= bgp
->peer_self
1931 PEER_STATUS_NSF_WAIT
))
1932 if (pi2
->peer
->status
1936 if (!aspath_cmp_left(pi1
->attr
->aspath
,
1938 && !aspath_cmp_left_confed(
1943 if (bgp_path_info_cmp(
1944 bgp
, pi2
, new_select
,
1945 &paths_eq
, mpath_cfg
, debug
,
1946 pfx_buf
, afi
, safi
)) {
1947 bgp_path_info_unset_flag(
1949 BGP_PATH_DMED_SELECTED
);
1953 bgp_path_info_set_flag(
1954 rn
, pi2
, BGP_PATH_DMED_CHECK
);
1957 bgp_path_info_set_flag(rn
, new_select
,
1958 BGP_PATH_DMED_CHECK
);
1959 bgp_path_info_set_flag(rn
, new_select
,
1960 BGP_PATH_DMED_SELECTED
);
1963 bgp_path_info_path_with_addpath_rx_str(
1964 new_select
, path_buf
);
1965 zlog_debug("%s: %s is the bestpath from AS %u",
1967 aspath_get_first_as(
1968 new_select
->attr
->aspath
));
1973 /* Check old selected route and new selected route. */
1976 for (pi
= bgp_node_get_bgp_path_info(rn
);
1977 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
1978 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
1981 if (BGP_PATH_HOLDDOWN(pi
)) {
1982 /* reap REMOVED routes, if needs be
1983 * selected route must stay for a while longer though
1985 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)
1986 && (pi
!= old_select
))
1987 bgp_path_info_reap(rn
, pi
);
1990 zlog_debug("%s: pi %p in holddown", __func__
,
1996 if (pi
->peer
&& pi
->peer
!= bgp
->peer_self
1997 && !CHECK_FLAG(pi
->peer
->sflags
, PEER_STATUS_NSF_WAIT
))
1998 if (pi
->peer
->status
!= Established
) {
2002 "%s: pi %p non self peer %s not estab state",
2003 __func__
, pi
, pi
->peer
->host
);
2008 if (bgp_flag_check(bgp
, BGP_FLAG_DETERMINISTIC_MED
)
2009 && (!CHECK_FLAG(pi
->flags
, BGP_PATH_DMED_SELECTED
))) {
2010 bgp_path_info_unset_flag(rn
, pi
, BGP_PATH_DMED_CHECK
);
2012 zlog_debug("%s: pi %p dmed", __func__
, pi
);
2016 bgp_path_info_unset_flag(rn
, pi
, BGP_PATH_DMED_CHECK
);
2018 if (bgp_path_info_cmp(bgp
, pi
, new_select
, &paths_eq
, mpath_cfg
,
2019 debug
, pfx_buf
, afi
, safi
)) {
2024 /* Now that we know which path is the bestpath see if any of the other
2026 * qualify as multipaths
2030 bgp_path_info_path_with_addpath_rx_str(new_select
,
2033 sprintf(path_buf
, "NONE");
2035 "%s: After path selection, newbest is %s oldbest was %s",
2037 old_select
? old_select
->peer
->host
: "NONE");
2040 if (do_mpath
&& new_select
) {
2041 for (pi
= bgp_node_get_bgp_path_info(rn
);
2042 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2045 bgp_path_info_path_with_addpath_rx_str(
2048 if (pi
== new_select
) {
2051 "%s: %s is the bestpath, add to the multipath list",
2053 bgp_mp_list_add(&mp_list
, pi
);
2057 if (BGP_PATH_HOLDDOWN(pi
))
2060 if (pi
->peer
&& pi
->peer
!= bgp
->peer_self
2061 && !CHECK_FLAG(pi
->peer
->sflags
,
2062 PEER_STATUS_NSF_WAIT
))
2063 if (pi
->peer
->status
!= Established
)
2066 if (!bgp_path_info_nexthop_cmp(pi
, new_select
)) {
2069 "%s: %s has the same nexthop as the bestpath, skip it",
2074 bgp_path_info_cmp(bgp
, pi
, new_select
, &paths_eq
,
2075 mpath_cfg
, debug
, pfx_buf
, afi
, safi
);
2080 "%s: %s is equivalent to the bestpath, add to the multipath list",
2082 bgp_mp_list_add(&mp_list
, pi
);
2087 bgp_path_info_mpath_update(rn
, new_select
, old_select
, &mp_list
,
2089 bgp_path_info_mpath_aggregate_update(new_select
, old_select
);
2090 bgp_mp_list_clear(&mp_list
);
2092 bgp_addpath_update_ids(bgp
, rn
, afi
, safi
);
2094 result
->old
= old_select
;
2095 result
->new = new_select
;
2101 * A new route/change in bestpath of an existing route. Evaluate the path
2102 * for advertisement to the subgroup.
2104 int subgroup_process_announce_selected(struct update_subgroup
*subgrp
,
2105 struct bgp_path_info
*selected
,
2106 struct bgp_node
*rn
,
2107 uint32_t addpath_tx_id
)
2110 struct peer
*onlypeer
;
2116 afi
= SUBGRP_AFI(subgrp
);
2117 safi
= SUBGRP_SAFI(subgrp
);
2118 onlypeer
= ((SUBGRP_PCOUNT(subgrp
) == 1) ? (SUBGRP_PFIRST(subgrp
))->peer
2121 if (BGP_DEBUG(update
, UPDATE_OUT
)) {
2122 char buf_prefix
[PREFIX_STRLEN
];
2123 prefix2str(p
, buf_prefix
, sizeof(buf_prefix
));
2124 zlog_debug("%s: p=%s, selected=%p", __func__
, buf_prefix
,
2128 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2129 if (onlypeer
&& CHECK_FLAG(onlypeer
->af_sflags
[afi
][safi
],
2130 PEER_STATUS_ORF_WAIT_REFRESH
))
2133 memset(&attr
, 0, sizeof(struct attr
));
2134 /* It's initialized in bgp_announce_check() */
2136 /* Announcement to the subgroup. If the route is filtered withdraw it.
2139 if (subgroup_announce_check(rn
, selected
, subgrp
, p
, &attr
))
2140 bgp_adj_out_set_subgroup(rn
, subgrp
, &attr
, selected
);
2142 bgp_adj_out_unset_subgroup(rn
, subgrp
, 1,
2146 /* If selected is NULL we must withdraw the path using addpath_tx_id */
2148 bgp_adj_out_unset_subgroup(rn
, subgrp
, 1, addpath_tx_id
);
2155 * Clear IGP changed flag and attribute changed flag for a route (all paths).
2156 * This is called at the end of route processing.
2158 void bgp_zebra_clear_route_change_flags(struct bgp_node
*rn
)
2160 struct bgp_path_info
*pi
;
2162 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
2163 if (BGP_PATH_HOLDDOWN(pi
))
2165 UNSET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2166 UNSET_FLAG(pi
->flags
, BGP_PATH_ATTR_CHANGED
);
2171 * Has the route changed from the RIB's perspective? This is invoked only
2172 * if the route selection returns the same best route as earlier - to
2173 * determine if we need to update zebra or not.
2175 int bgp_zebra_has_route_changed(struct bgp_node
*rn
,
2176 struct bgp_path_info
*selected
)
2178 struct bgp_path_info
*mpinfo
;
2180 /* If this is multipath, check all selected paths for any nexthop
2181 * change or attribute change. Some attribute changes (e.g., community)
2182 * aren't of relevance to the RIB, but we'll update zebra to ensure
2183 * we handle the case of BGP nexthop change. This is the behavior
2184 * when the best path has an attribute change anyway.
2186 if (CHECK_FLAG(selected
->flags
, BGP_PATH_IGP_CHANGED
)
2187 || CHECK_FLAG(selected
->flags
, BGP_PATH_MULTIPATH_CHG
))
2191 * If this is multipath, check all selected paths for any nexthop change
2193 for (mpinfo
= bgp_path_info_mpath_first(selected
); mpinfo
;
2194 mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
2195 if (CHECK_FLAG(mpinfo
->flags
, BGP_PATH_IGP_CHANGED
)
2196 || CHECK_FLAG(mpinfo
->flags
, BGP_PATH_ATTR_CHANGED
))
2200 /* Nothing has changed from the RIB's perspective. */
2204 struct bgp_process_queue
{
2206 STAILQ_HEAD(, bgp_node
) pqueue
;
2207 #define BGP_PROCESS_QUEUE_EOIU_MARKER (1 << 0)
2209 unsigned int queued
;
2213 * old_select = The old best path
2214 * new_select = the new best path
2216 * if (!old_select && new_select)
2217 * We are sending new information on.
2219 * if (old_select && new_select) {
2220 * if (new_select != old_select)
2221 * We have a new best path send a change
2223 * We've received a update with new attributes that needs
2227 * if (old_select && !new_select)
2228 * We have no eligible route that we can announce or the rn
2231 static void bgp_process_main_one(struct bgp
*bgp
, struct bgp_node
*rn
,
2232 afi_t afi
, safi_t safi
)
2234 struct bgp_path_info
*new_select
;
2235 struct bgp_path_info
*old_select
;
2236 struct bgp_path_info_pair old_and_new
;
2237 char pfx_buf
[PREFIX2STR_BUFFER
];
2240 /* Is it end of initial update? (after startup) */
2242 quagga_timestamp(3, bgp
->update_delay_zebra_resume_time
,
2243 sizeof(bgp
->update_delay_zebra_resume_time
));
2245 bgp
->main_zebra_update_hold
= 0;
2246 FOREACH_AFI_SAFI (afi
, safi
) {
2247 if (bgp_fibupd_safi(safi
))
2248 bgp_zebra_announce_table(bgp
, afi
, safi
);
2250 bgp
->main_peers_update_hold
= 0;
2252 bgp_start_routeadv(bgp
);
2256 struct prefix
*p
= &rn
->p
;
2258 debug
= bgp_debug_bestpath(&rn
->p
);
2260 prefix2str(&rn
->p
, pfx_buf
, sizeof(pfx_buf
));
2261 zlog_debug("%s: p=%s afi=%s, safi=%s start", __func__
, pfx_buf
,
2262 afi2str(afi
), safi2str(safi
));
2265 /* Best path selection. */
2266 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
2268 old_select
= old_and_new
.old
;
2269 new_select
= old_and_new
.new;
2271 /* Do we need to allocate or free labels?
2272 * Right now, since we only deal with per-prefix labels, it is not
2273 * necessary to do this upon changes to best path. Exceptions:
2274 * - label index has changed -> recalculate resulting label
2275 * - path_info sub_type changed -> switch to/from implicit-null
2276 * - no valid label (due to removed static label binding) -> get new one
2278 if (bgp
->allocate_mpls_labels
[afi
][safi
]) {
2281 || bgp_label_index_differs(new_select
, old_select
)
2282 || new_select
->sub_type
!= old_select
->sub_type
2283 || !bgp_is_valid_label(&rn
->local_label
)) {
2284 /* Enforced penultimate hop popping:
2285 * implicit-null for local routes, aggregate
2286 * and redistributed routes
2288 if (new_select
->sub_type
== BGP_ROUTE_STATIC
2289 || new_select
->sub_type
2290 == BGP_ROUTE_AGGREGATE
2291 || new_select
->sub_type
2292 == BGP_ROUTE_REDISTRIBUTE
) {
2295 BGP_NODE_REGISTERED_FOR_LABEL
))
2296 bgp_unregister_for_label(rn
);
2297 label_ntop(MPLS_LABEL_IMPLICIT_NULL
, 1,
2299 bgp_set_valid_label(&rn
->local_label
);
2301 bgp_register_for_label(rn
, new_select
);
2303 } else if (CHECK_FLAG(rn
->flags
,
2304 BGP_NODE_REGISTERED_FOR_LABEL
)) {
2305 bgp_unregister_for_label(rn
);
2307 } else if (CHECK_FLAG(rn
->flags
, BGP_NODE_REGISTERED_FOR_LABEL
)) {
2308 bgp_unregister_for_label(rn
);
2312 prefix2str(&rn
->p
, pfx_buf
, sizeof(pfx_buf
));
2314 "%s: p=%s afi=%s, safi=%s, old_select=%p, new_select=%p",
2315 __func__
, pfx_buf
, afi2str(afi
), safi2str(safi
),
2316 old_select
, new_select
);
2319 /* If best route remains the same and this is not due to user-initiated
2320 * clear, see exactly what needs to be done.
2322 if (old_select
&& old_select
== new_select
2323 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
2324 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
2325 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
2326 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
2328 vnc_import_bgp_add_route(bgp
, p
, old_select
);
2329 vnc_import_bgp_exterior_add_route(bgp
, p
, old_select
);
2331 if (bgp_fibupd_safi(safi
)
2332 && !bgp_option_check(BGP_OPT_NO_FIB
)) {
2334 if (new_select
->type
== ZEBRA_ROUTE_BGP
2335 && (new_select
->sub_type
== BGP_ROUTE_NORMAL
2336 || new_select
->sub_type
2337 == BGP_ROUTE_IMPORTED
))
2339 bgp_zebra_announce(rn
, p
, old_select
,
2343 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
2344 bgp_zebra_clear_route_change_flags(rn
);
2346 /* If there is a change of interest to peers, reannounce the
2348 if (CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
2349 || CHECK_FLAG(rn
->flags
, BGP_NODE_LABEL_CHANGED
)) {
2350 group_announce_route(bgp
, afi
, safi
, rn
, new_select
);
2352 /* unicast routes must also be annouced to
2353 * labeled-unicast update-groups */
2354 if (safi
== SAFI_UNICAST
)
2355 group_announce_route(bgp
, afi
,
2356 SAFI_LABELED_UNICAST
, rn
,
2359 UNSET_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
);
2360 UNSET_FLAG(rn
->flags
, BGP_NODE_LABEL_CHANGED
);
2363 UNSET_FLAG(rn
->flags
, BGP_NODE_PROCESS_SCHEDULED
);
2367 /* If the user did "clear ip bgp prefix x.x.x.x" this flag will be set
2369 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
2371 /* bestpath has changed; bump version */
2372 if (old_select
|| new_select
) {
2373 bgp_bump_version(rn
);
2375 if (!bgp
->t_rmap_def_originate_eval
) {
2379 update_group_refresh_default_originate_route_map
,
2380 bgp
, RMAP_DEFAULT_ORIGINATE_EVAL_TIMER
,
2381 &bgp
->t_rmap_def_originate_eval
);
2386 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
2389 zlog_debug("%s: setting SELECTED flag", __func__
);
2390 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
2391 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
2392 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
2396 if ((afi
== AFI_IP
|| afi
== AFI_IP6
) && (safi
== SAFI_UNICAST
)) {
2397 if (old_select
!= new_select
) {
2399 vnc_import_bgp_exterior_del_route(bgp
, p
,
2401 vnc_import_bgp_del_route(bgp
, p
, old_select
);
2404 vnc_import_bgp_exterior_add_route(bgp
, p
,
2406 vnc_import_bgp_add_route(bgp
, p
, new_select
);
2412 group_announce_route(bgp
, afi
, safi
, rn
, new_select
);
2414 /* unicast routes must also be annouced to labeled-unicast update-groups
2416 if (safi
== SAFI_UNICAST
)
2417 group_announce_route(bgp
, afi
, SAFI_LABELED_UNICAST
, rn
,
2421 if (bgp_fibupd_safi(safi
) && (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
2422 && !bgp_option_check(BGP_OPT_NO_FIB
)) {
2423 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
2424 && (new_select
->sub_type
== BGP_ROUTE_NORMAL
2425 || new_select
->sub_type
== BGP_ROUTE_AGGREGATE
2426 || new_select
->sub_type
== BGP_ROUTE_IMPORTED
)) {
2428 /* if this is an evpn imported type-5 prefix,
2429 * we need to withdraw the route first to clear
2430 * the nh neigh and the RMAC entry.
2433 is_route_parent_evpn(old_select
))
2434 bgp_zebra_withdraw(p
, old_select
, bgp
, safi
);
2436 bgp_zebra_announce(rn
, p
, new_select
, bgp
, afi
, safi
);
2438 /* Withdraw the route from the kernel. */
2439 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
2440 && (old_select
->sub_type
== BGP_ROUTE_NORMAL
2441 || old_select
->sub_type
== BGP_ROUTE_AGGREGATE
2442 || old_select
->sub_type
== BGP_ROUTE_IMPORTED
))
2444 bgp_zebra_withdraw(p
, old_select
, bgp
, safi
);
2448 /* advertise/withdraw type-5 routes */
2449 if ((afi
== AFI_IP
|| afi
== AFI_IP6
) && (safi
== SAFI_UNICAST
)) {
2450 if (advertise_type5_routes(bgp
, afi
) && new_select
&&
2451 (!new_select
->extra
|| !new_select
->extra
->parent
)) {
2453 /* apply the route-map */
2454 if (bgp
->adv_cmd_rmap
[afi
][safi
].map
) {
2457 ret
= route_map_apply(
2458 bgp
->adv_cmd_rmap
[afi
][safi
].map
,
2459 &rn
->p
, RMAP_BGP
, new_select
);
2460 if (ret
== RMAP_MATCH
)
2461 bgp_evpn_advertise_type5_route(
2462 bgp
, &rn
->p
, new_select
->attr
,
2465 bgp_evpn_advertise_type5_route(bgp
,
2471 } else if (advertise_type5_routes(bgp
, afi
) && old_select
&&
2472 (!old_select
->extra
|| !old_select
->extra
->parent
))
2473 bgp_evpn_withdraw_type5_route(bgp
, &rn
->p
, afi
, safi
);
2476 /* Clear any route change flags. */
2477 bgp_zebra_clear_route_change_flags(rn
);
2479 /* Reap old select bgp_path_info, if it has been removed */
2480 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
2481 bgp_path_info_reap(rn
, old_select
);
2483 UNSET_FLAG(rn
->flags
, BGP_NODE_PROCESS_SCHEDULED
);
2487 static wq_item_status
bgp_process_wq(struct work_queue
*wq
, void *data
)
2489 struct bgp_process_queue
*pqnode
= data
;
2490 struct bgp
*bgp
= pqnode
->bgp
;
2491 struct bgp_table
*table
;
2492 struct bgp_node
*rn
;
2495 if (CHECK_FLAG(pqnode
->flags
, BGP_PROCESS_QUEUE_EOIU_MARKER
)) {
2496 bgp_process_main_one(bgp
, NULL
, 0, 0);
2497 /* should always have dedicated wq call */
2498 assert(STAILQ_FIRST(&pqnode
->pqueue
) == NULL
);
2502 while (!STAILQ_EMPTY(&pqnode
->pqueue
)) {
2503 rn
= STAILQ_FIRST(&pqnode
->pqueue
);
2504 STAILQ_REMOVE_HEAD(&pqnode
->pqueue
, pq
);
2505 STAILQ_NEXT(rn
, pq
) = NULL
; /* complete unlink */
2506 table
= bgp_node_table(rn
);
2507 /* note, new RNs may be added as part of processing */
2508 bgp_process_main_one(bgp
, rn
, table
->afi
, table
->safi
);
2510 bgp_unlock_node(rn
);
2511 bgp_table_unlock(table
);
2517 static void bgp_processq_del(struct work_queue
*wq
, void *data
)
2519 struct bgp_process_queue
*pqnode
= data
;
2521 bgp_unlock(pqnode
->bgp
);
2523 XFREE(MTYPE_BGP_PROCESS_QUEUE
, pqnode
);
2526 void bgp_process_queue_init(void)
2528 if (!bm
->process_main_queue
)
2529 bm
->process_main_queue
=
2530 work_queue_new(bm
->master
, "process_main_queue");
2532 bm
->process_main_queue
->spec
.workfunc
= &bgp_process_wq
;
2533 bm
->process_main_queue
->spec
.del_item_data
= &bgp_processq_del
;
2534 bm
->process_main_queue
->spec
.max_retries
= 0;
2535 bm
->process_main_queue
->spec
.hold
= 50;
2536 /* Use a higher yield value of 50ms for main queue processing */
2537 bm
->process_main_queue
->spec
.yield
= 50 * 1000L;
2540 static struct bgp_process_queue
*bgp_processq_alloc(struct bgp
*bgp
)
2542 struct bgp_process_queue
*pqnode
;
2544 pqnode
= XCALLOC(MTYPE_BGP_PROCESS_QUEUE
,
2545 sizeof(struct bgp_process_queue
));
2547 /* unlocked in bgp_processq_del */
2548 pqnode
->bgp
= bgp_lock(bgp
);
2549 STAILQ_INIT(&pqnode
->pqueue
);
2554 void bgp_process(struct bgp
*bgp
, struct bgp_node
*rn
, afi_t afi
, safi_t safi
)
2556 #define ARBITRARY_PROCESS_QLEN 10000
2557 struct work_queue
*wq
= bm
->process_main_queue
;
2558 struct bgp_process_queue
*pqnode
;
2559 int pqnode_reuse
= 0;
2561 /* already scheduled for processing? */
2562 if (CHECK_FLAG(rn
->flags
, BGP_NODE_PROCESS_SCHEDULED
))
2568 /* Add route nodes to an existing work queue item until reaching the
2569 limit only if is from the same BGP view and it's not an EOIU marker
2571 if (work_queue_item_count(wq
)) {
2572 struct work_queue_item
*item
= work_queue_last_item(wq
);
2573 pqnode
= item
->data
;
2575 if (CHECK_FLAG(pqnode
->flags
, BGP_PROCESS_QUEUE_EOIU_MARKER
)
2576 || pqnode
->bgp
!= bgp
2577 || pqnode
->queued
>= ARBITRARY_PROCESS_QLEN
)
2578 pqnode
= bgp_processq_alloc(bgp
);
2582 pqnode
= bgp_processq_alloc(bgp
);
2583 /* all unlocked in bgp_process_wq */
2584 bgp_table_lock(bgp_node_table(rn
));
2586 SET_FLAG(rn
->flags
, BGP_NODE_PROCESS_SCHEDULED
);
2589 /* can't be enqueued twice */
2590 assert(STAILQ_NEXT(rn
, pq
) == NULL
);
2591 STAILQ_INSERT_TAIL(&pqnode
->pqueue
, rn
, pq
);
2595 work_queue_add(wq
, pqnode
);
2600 void bgp_add_eoiu_mark(struct bgp
*bgp
)
2602 struct bgp_process_queue
*pqnode
;
2604 if (bm
->process_main_queue
== NULL
)
2607 pqnode
= bgp_processq_alloc(bgp
);
2609 SET_FLAG(pqnode
->flags
, BGP_PROCESS_QUEUE_EOIU_MARKER
);
2610 work_queue_add(bm
->process_main_queue
, pqnode
);
2613 static int bgp_maximum_prefix_restart_timer(struct thread
*thread
)
2617 peer
= THREAD_ARG(thread
);
2618 peer
->t_pmax_restart
= NULL
;
2620 if (bgp_debug_neighbor_events(peer
))
2622 "%s Maximum-prefix restart timer expired, restore peering",
2625 if ((peer_clear(peer
, NULL
) < 0) && bgp_debug_neighbor_events(peer
))
2626 zlog_debug("%s: %s peer_clear failed",
2627 __PRETTY_FUNCTION__
, peer
->host
);
2632 int bgp_maximum_prefix_overflow(struct peer
*peer
, afi_t afi
, safi_t safi
,
2636 iana_safi_t pkt_safi
;
2638 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX
))
2641 if (peer
->pcount
[afi
][safi
] > peer
->pmax
[afi
][safi
]) {
2642 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2643 PEER_STATUS_PREFIX_LIMIT
)
2648 "%%MAXPFXEXCEED: No. of %s prefix received from %s %ld exceed, "
2650 afi_safi_print(afi
, safi
), peer
->host
,
2651 peer
->pcount
[afi
][safi
], peer
->pmax
[afi
][safi
]);
2652 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_PREFIX_LIMIT
);
2654 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2655 PEER_FLAG_MAX_PREFIX_WARNING
))
2658 /* Convert AFI, SAFI to values for packet. */
2659 pkt_afi
= afi_int2iana(afi
);
2660 pkt_safi
= safi_int2iana(safi
);
2664 ndata
[0] = (pkt_afi
>> 8);
2666 ndata
[2] = pkt_safi
;
2667 ndata
[3] = (peer
->pmax
[afi
][safi
] >> 24);
2668 ndata
[4] = (peer
->pmax
[afi
][safi
] >> 16);
2669 ndata
[5] = (peer
->pmax
[afi
][safi
] >> 8);
2670 ndata
[6] = (peer
->pmax
[afi
][safi
]);
2672 SET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
2673 bgp_notify_send_with_data(peer
, BGP_NOTIFY_CEASE
,
2674 BGP_NOTIFY_CEASE_MAX_PREFIX
,
2678 /* Dynamic peers will just close their connection. */
2679 if (peer_dynamic_neighbor(peer
))
2682 /* restart timer start */
2683 if (peer
->pmax_restart
[afi
][safi
]) {
2684 peer
->v_pmax_restart
=
2685 peer
->pmax_restart
[afi
][safi
] * 60;
2687 if (bgp_debug_neighbor_events(peer
))
2689 "%s Maximum-prefix restart timer started for %d secs",
2690 peer
->host
, peer
->v_pmax_restart
);
2692 BGP_TIMER_ON(peer
->t_pmax_restart
,
2693 bgp_maximum_prefix_restart_timer
,
2694 peer
->v_pmax_restart
);
2699 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2700 PEER_STATUS_PREFIX_LIMIT
);
2702 if (peer
->pcount
[afi
][safi
]
2703 > (peer
->pmax
[afi
][safi
] * peer
->pmax_threshold
[afi
][safi
] / 100)) {
2704 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2705 PEER_STATUS_PREFIX_THRESHOLD
)
2710 "%%MAXPFX: No. of %s prefix received from %s reaches %ld, max %ld",
2711 afi_safi_print(afi
, safi
), peer
->host
,
2712 peer
->pcount
[afi
][safi
], peer
->pmax
[afi
][safi
]);
2713 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2714 PEER_STATUS_PREFIX_THRESHOLD
);
2716 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2717 PEER_STATUS_PREFIX_THRESHOLD
);
2721 /* Unconditionally remove the route from the RIB, without taking
2722 * damping into consideration (eg, because the session went down)
2724 void bgp_rib_remove(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
2725 struct peer
*peer
, afi_t afi
, safi_t safi
)
2727 bgp_aggregate_decrement(peer
->bgp
, &rn
->p
, pi
, afi
, safi
);
2729 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
))
2730 bgp_path_info_delete(rn
, pi
); /* keep historical info */
2732 bgp_process(peer
->bgp
, rn
, afi
, safi
);
2735 static void bgp_rib_withdraw(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
2736 struct peer
*peer
, afi_t afi
, safi_t safi
,
2737 struct prefix_rd
*prd
)
2739 /* apply dampening, if result is suppressed, we'll be retaining
2740 * the bgp_path_info in the RIB for historical reference.
2742 if (CHECK_FLAG(peer
->bgp
->af_flags
[afi
][safi
], BGP_CONFIG_DAMPENING
)
2743 && peer
->sort
== BGP_PEER_EBGP
)
2744 if ((bgp_damp_withdraw(pi
, rn
, afi
, safi
, 0))
2745 == BGP_DAMP_SUPPRESSED
) {
2746 bgp_aggregate_decrement(peer
->bgp
, &rn
->p
, pi
, afi
,
2752 if (safi
== SAFI_MPLS_VPN
) {
2753 struct bgp_node
*prn
= NULL
;
2754 struct bgp_table
*table
= NULL
;
2756 prn
= bgp_node_get(peer
->bgp
->rib
[afi
][safi
],
2757 (struct prefix
*)prd
);
2758 if (bgp_node_has_bgp_path_info_data(prn
)) {
2759 table
= bgp_node_get_bgp_table_info(prn
);
2761 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
2762 peer
->bgp
, prd
, table
, &rn
->p
, pi
);
2764 bgp_unlock_node(prn
);
2766 if ((afi
== AFI_IP
|| afi
== AFI_IP6
) && (safi
== SAFI_UNICAST
)) {
2767 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2769 vnc_import_bgp_del_route(peer
->bgp
, &rn
->p
, pi
);
2770 vnc_import_bgp_exterior_del_route(peer
->bgp
, &rn
->p
,
2776 /* If this is an EVPN route, process for un-import. */
2777 if (safi
== SAFI_EVPN
)
2778 bgp_evpn_unimport_route(peer
->bgp
, afi
, safi
, &rn
->p
, pi
);
2780 bgp_rib_remove(rn
, pi
, peer
, afi
, safi
);
2783 struct bgp_path_info
*info_make(int type
, int sub_type
, unsigned short instance
,
2784 struct peer
*peer
, struct attr
*attr
,
2785 struct bgp_node
*rn
)
2787 struct bgp_path_info
*new;
2789 /* Make new BGP info. */
2790 new = XCALLOC(MTYPE_BGP_ROUTE
, sizeof(struct bgp_path_info
));
2792 new->instance
= instance
;
2793 new->sub_type
= sub_type
;
2796 new->uptime
= bgp_clock();
2801 static void overlay_index_update(struct attr
*attr
,
2802 struct eth_segment_id
*eth_s_id
,
2803 union gw_addr
*gw_ip
)
2808 if (eth_s_id
== NULL
) {
2809 memset(&(attr
->evpn_overlay
.eth_s_id
), 0,
2810 sizeof(struct eth_segment_id
));
2812 memcpy(&(attr
->evpn_overlay
.eth_s_id
), eth_s_id
,
2813 sizeof(struct eth_segment_id
));
2815 if (gw_ip
== NULL
) {
2816 memset(&(attr
->evpn_overlay
.gw_ip
), 0, sizeof(union gw_addr
));
2818 memcpy(&(attr
->evpn_overlay
.gw_ip
), gw_ip
,
2819 sizeof(union gw_addr
));
2823 static bool overlay_index_equal(afi_t afi
, struct bgp_path_info
*path
,
2824 struct eth_segment_id
*eth_s_id
,
2825 union gw_addr
*gw_ip
)
2827 struct eth_segment_id
*path_eth_s_id
, *path_eth_s_id_remote
;
2828 union gw_addr
*path_gw_ip
, *path_gw_ip_remote
;
2830 struct eth_segment_id esi
;
2834 if (afi
!= AFI_L2VPN
)
2837 memset(&temp
, 0, sizeof(temp
));
2838 path_eth_s_id
= &temp
.esi
;
2839 path_gw_ip
= &temp
.ip
;
2841 if (eth_s_id
== NULL
&& gw_ip
== NULL
)
2844 path_eth_s_id
= &(path
->attr
->evpn_overlay
.eth_s_id
);
2845 path_gw_ip
= &(path
->attr
->evpn_overlay
.gw_ip
);
2848 if (gw_ip
== NULL
) {
2849 memset(&temp
, 0, sizeof(temp
));
2850 path_gw_ip_remote
= &temp
.ip
;
2852 path_gw_ip_remote
= gw_ip
;
2854 if (eth_s_id
== NULL
) {
2855 memset(&temp
, 0, sizeof(temp
));
2856 path_eth_s_id_remote
= &temp
.esi
;
2858 path_eth_s_id_remote
= eth_s_id
;
2860 if (!memcmp(path_gw_ip
, path_gw_ip_remote
, sizeof(union gw_addr
)))
2863 return !memcmp(path_eth_s_id
, path_eth_s_id_remote
,
2864 sizeof(struct eth_segment_id
));
2867 /* Check if received nexthop is valid or not. */
2868 static int bgp_update_martian_nexthop(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2873 /* Only validated for unicast and multicast currently. */
2874 /* Also valid for EVPN where the nexthop is an IP address. */
2875 if (safi
!= SAFI_UNICAST
&& safi
!= SAFI_MULTICAST
&& safi
!= SAFI_EVPN
)
2878 /* If NEXT_HOP is present, validate it. */
2879 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
2880 if (attr
->nexthop
.s_addr
== 0
2881 || IPV4_CLASS_DE(ntohl(attr
->nexthop
.s_addr
))
2882 || bgp_nexthop_self(bgp
, attr
->nexthop
))
2886 /* If MP_NEXTHOP is present, validate it. */
2887 /* Note: For IPv6 nexthops, we only validate the global (1st) nexthop;
2888 * there is code in bgp_attr.c to ignore the link-local (2nd) nexthop if
2889 * it is not an IPv6 link-local address.
2891 if (attr
->mp_nexthop_len
) {
2892 switch (attr
->mp_nexthop_len
) {
2893 case BGP_ATTR_NHLEN_IPV4
:
2894 case BGP_ATTR_NHLEN_VPNV4
:
2895 ret
= (attr
->mp_nexthop_global_in
.s_addr
== 0
2896 || IPV4_CLASS_DE(ntohl(
2897 attr
->mp_nexthop_global_in
.s_addr
))
2898 || bgp_nexthop_self(bgp
,
2899 attr
->mp_nexthop_global_in
));
2902 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
2903 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
2904 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
2905 ret
= (IN6_IS_ADDR_UNSPECIFIED(&attr
->mp_nexthop_global
)
2906 || IN6_IS_ADDR_LOOPBACK(&attr
->mp_nexthop_global
)
2907 || IN6_IS_ADDR_MULTICAST(
2908 &attr
->mp_nexthop_global
));
2920 int bgp_update(struct peer
*peer
, struct prefix
*p
, uint32_t addpath_id
,
2921 struct attr
*attr
, afi_t afi
, safi_t safi
, int type
,
2922 int sub_type
, struct prefix_rd
*prd
, mpls_label_t
*label
,
2923 uint32_t num_labels
, int soft_reconfig
,
2924 struct bgp_route_evpn
*evpn
)
2927 int aspath_loop_count
= 0;
2928 struct bgp_node
*rn
;
2930 struct attr new_attr
;
2931 struct attr
*attr_new
;
2932 struct bgp_path_info
*pi
;
2933 struct bgp_path_info
*new;
2934 struct bgp_path_info_extra
*extra
;
2936 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
2938 int do_loop_check
= 1;
2939 int has_valid_label
= 0;
2941 int vnc_implicit_withdraw
= 0;
2945 memset(&new_attr
, 0, sizeof(struct attr
));
2946 new_attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
2947 new_attr
.label
= MPLS_INVALID_LABEL
;
2950 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, prd
);
2951 /* TODO: Check to see if we can get rid of "is_valid_label" */
2952 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)
2953 has_valid_label
= (num_labels
> 0) ? 1 : 0;
2955 has_valid_label
= bgp_is_valid_label(label
);
2957 /* When peer's soft reconfiguration enabled. Record input packet in
2960 && CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SOFT_RECONFIG
)
2961 && peer
!= bgp
->peer_self
)
2962 bgp_adj_in_set(rn
, peer
, attr
, addpath_id
);
2964 /* Check previously received route. */
2965 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2966 if (pi
->peer
== peer
&& pi
->type
== type
2967 && pi
->sub_type
== sub_type
2968 && pi
->addpath_rx_id
== addpath_id
)
2971 /* AS path local-as loop check. */
2972 if (peer
->change_local_as
) {
2973 if (peer
->allowas_in
[afi
][safi
])
2974 aspath_loop_count
= peer
->allowas_in
[afi
][safi
];
2975 else if (!CHECK_FLAG(peer
->flags
,
2976 PEER_FLAG_LOCAL_AS_NO_PREPEND
))
2977 aspath_loop_count
= 1;
2979 if (aspath_loop_check(attr
->aspath
, peer
->change_local_as
)
2980 > aspath_loop_count
) {
2981 reason
= "as-path contains our own AS;";
2986 /* If the peer is configured for "allowas-in origin" and the last ASN in
2988 * as-path is our ASN then we do not need to call aspath_loop_check
2990 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN_ORIGIN
))
2991 if (aspath_get_last_as(attr
->aspath
) == bgp
->as
)
2994 /* AS path loop check. */
2995 if (do_loop_check
) {
2996 if (aspath_loop_check(attr
->aspath
, bgp
->as
)
2997 > peer
->allowas_in
[afi
][safi
]
2998 || (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)
2999 && aspath_loop_check(attr
->aspath
, bgp
->confed_id
)
3000 > peer
->allowas_in
[afi
][safi
])) {
3001 reason
= "as-path contains our own AS;";
3006 /* Route reflector originator ID check. */
3007 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
)
3008 && IPV4_ADDR_SAME(&bgp
->router_id
, &attr
->originator_id
)) {
3009 reason
= "originator is us;";
3013 /* Route reflector cluster ID check. */
3014 if (bgp_cluster_filter(peer
, attr
)) {
3015 reason
= "reflected from the same cluster;";
3019 /* Apply incoming filter. */
3020 if (bgp_input_filter(peer
, p
, attr
, afi
, safi
) == FILTER_DENY
) {
3025 bgp_attr_dup(&new_attr
, attr
);
3027 /* Apply incoming route-map.
3028 * NB: new_attr may now contain newly allocated values from route-map
3030 * commands, so we need bgp_attr_flush in the error paths, until we
3032 * the attr (which takes over the memory references) */
3033 if (bgp_input_modifier(peer
, p
, &new_attr
, afi
, safi
, NULL
)
3035 reason
= "route-map;";
3036 bgp_attr_flush(&new_attr
);
3040 if (peer
->sort
== BGP_PEER_EBGP
) {
3042 /* If we receive the graceful-shutdown community from an eBGP
3043 * peer we must lower local-preference */
3044 if (new_attr
.community
3045 && community_include(new_attr
.community
, COMMUNITY_GSHUT
)) {
3046 new_attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
3047 new_attr
.local_pref
= BGP_GSHUT_LOCAL_PREF
;
3049 /* If graceful-shutdown is configured then add the GSHUT
3050 * community to all paths received from eBGP peers */
3051 } else if (bgp_flag_check(peer
->bgp
,
3052 BGP_FLAG_GRACEFUL_SHUTDOWN
)) {
3053 bgp_attr_add_gshut_community(&new_attr
);
3057 /* next hop check. */
3058 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
)
3059 && bgp_update_martian_nexthop(bgp
, afi
, safi
, &new_attr
)) {
3060 reason
= "martian or self next-hop;";
3061 bgp_attr_flush(&new_attr
);
3065 if (bgp_mac_entry_exists(p
)) {
3066 reason
= "self mac;";
3070 attr_new
= bgp_attr_intern(&new_attr
);
3072 /* If the update is implicit withdraw. */
3074 pi
->uptime
= bgp_clock();
3075 same_attr
= attrhash_cmp(pi
->attr
, attr_new
);
3077 /* Same attribute comes in. */
3078 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)
3079 && attrhash_cmp(pi
->attr
, attr_new
)
3080 && (!has_valid_label
3081 || memcmp(&(bgp_path_info_extra_get(pi
))->label
, label
,
3082 num_labels
* sizeof(mpls_label_t
))
3084 && (overlay_index_equal(
3085 afi
, pi
, evpn
== NULL
? NULL
: &evpn
->eth_s_id
,
3086 evpn
== NULL
? NULL
: &evpn
->gw_ip
))) {
3087 if (CHECK_FLAG(bgp
->af_flags
[afi
][safi
],
3088 BGP_CONFIG_DAMPENING
)
3089 && peer
->sort
== BGP_PEER_EBGP
3090 && CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
)) {
3091 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3092 bgp_debug_rdpfxpath2str(
3093 afi
, safi
, prd
, p
, label
,
3094 num_labels
, addpath_id
? 1 : 0,
3095 addpath_id
, pfx_buf
,
3097 zlog_debug("%s rcvd %s", peer
->host
,
3101 if (bgp_damp_update(pi
, rn
, afi
, safi
)
3102 != BGP_DAMP_SUPPRESSED
) {
3103 bgp_aggregate_increment(bgp
, p
, pi
, afi
,
3105 bgp_process(bgp
, rn
, afi
, safi
);
3107 } else /* Duplicate - odd */
3109 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3110 if (!peer
->rcvd_attr_printed
) {
3112 "%s rcvd UPDATE w/ attr: %s",
3114 peer
->rcvd_attr_str
);
3115 peer
->rcvd_attr_printed
= 1;
3118 bgp_debug_rdpfxpath2str(
3119 afi
, safi
, prd
, p
, label
,
3120 num_labels
, addpath_id
? 1 : 0,
3121 addpath_id
, pfx_buf
,
3124 "%s rcvd %s...duplicate ignored",
3125 peer
->host
, pfx_buf
);
3128 /* graceful restart STALE flag unset. */
3129 if (CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
)) {
3130 bgp_path_info_unset_flag(
3131 rn
, pi
, BGP_PATH_STALE
);
3132 bgp_process(bgp
, rn
, afi
, safi
);
3136 bgp_unlock_node(rn
);
3137 bgp_attr_unintern(&attr_new
);
3142 /* Withdraw/Announce before we fully processed the withdraw */
3143 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
3144 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3145 bgp_debug_rdpfxpath2str(
3146 afi
, safi
, prd
, p
, label
, num_labels
,
3147 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3150 "%s rcvd %s, flapped quicker than processing",
3151 peer
->host
, pfx_buf
);
3154 bgp_path_info_restore(rn
, pi
);
3157 /* Received Logging. */
3158 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3159 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
,
3160 num_labels
, addpath_id
? 1 : 0,
3161 addpath_id
, pfx_buf
,
3163 zlog_debug("%s rcvd %s", peer
->host
, pfx_buf
);
3166 /* graceful restart STALE flag unset. */
3167 if (CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
))
3168 bgp_path_info_unset_flag(rn
, pi
, BGP_PATH_STALE
);
3170 /* The attribute is changed. */
3171 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
3173 /* implicit withdraw, decrement aggregate and pcount here.
3174 * only if update is accepted, they'll increment below.
3176 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
3178 /* Update bgp route dampening information. */
3179 if (CHECK_FLAG(bgp
->af_flags
[afi
][safi
], BGP_CONFIG_DAMPENING
)
3180 && peer
->sort
== BGP_PEER_EBGP
) {
3181 /* This is implicit withdraw so we should update
3184 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
))
3185 bgp_damp_withdraw(pi
, rn
, afi
, safi
, 1);
3188 if (safi
== SAFI_MPLS_VPN
) {
3189 struct bgp_node
*prn
= NULL
;
3190 struct bgp_table
*table
= NULL
;
3192 prn
= bgp_node_get(bgp
->rib
[afi
][safi
],
3193 (struct prefix
*)prd
);
3194 if (bgp_node_has_bgp_path_info_data(prn
)) {
3195 table
= bgp_node_get_bgp_table_info(prn
);
3197 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
3198 bgp
, prd
, table
, p
, pi
);
3200 bgp_unlock_node(prn
);
3202 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
3203 && (safi
== SAFI_UNICAST
)) {
3204 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
3206 * Implicit withdraw case.
3208 ++vnc_implicit_withdraw
;
3209 vnc_import_bgp_del_route(bgp
, p
, pi
);
3210 vnc_import_bgp_exterior_del_route(bgp
, p
, pi
);
3215 /* Special handling for EVPN update of an existing route. If the
3216 * extended community attribute has changed, we need to
3218 * the route using its existing extended community. It will be
3219 * subsequently processed for import with the new extended
3222 if (safi
== SAFI_EVPN
&& !same_attr
) {
3224 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))
3226 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))) {
3229 cmp
= ecommunity_cmp(pi
->attr
->ecommunity
,
3230 attr_new
->ecommunity
);
3232 if (bgp_debug_update(peer
, p
, NULL
, 1))
3234 "Change in EXT-COMM, existing %s new %s",
3236 pi
->attr
->ecommunity
),
3238 attr_new
->ecommunity
));
3239 bgp_evpn_unimport_route(bgp
, afi
, safi
,
3245 /* Update to new attribute. */
3246 bgp_attr_unintern(&pi
->attr
);
3247 pi
->attr
= attr_new
;
3249 /* Update MPLS label */
3250 if (has_valid_label
) {
3251 extra
= bgp_path_info_extra_get(pi
);
3252 if (extra
->label
!= label
) {
3253 memcpy(&extra
->label
, label
,
3254 num_labels
* sizeof(mpls_label_t
));
3255 extra
->num_labels
= num_labels
;
3257 if (!(afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
))
3258 bgp_set_valid_label(&extra
->label
[0]);
3262 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
3263 && (safi
== SAFI_UNICAST
)) {
3264 if (vnc_implicit_withdraw
) {
3266 * Add back the route with its new attributes
3268 * The route is still selected, until the route
3270 * queued by bgp_process actually runs. We have
3272 * update to the VNC side immediately to avoid
3274 * configuration changes (e.g., route-map
3276 * trigger re-importation of the entire RIB.
3278 vnc_import_bgp_add_route(bgp
, p
, pi
);
3279 vnc_import_bgp_exterior_add_route(bgp
, p
, pi
);
3283 /* Update Overlay Index */
3284 if (afi
== AFI_L2VPN
) {
3285 overlay_index_update(
3286 pi
->attr
, evpn
== NULL
? NULL
: &evpn
->eth_s_id
,
3287 evpn
== NULL
? NULL
: &evpn
->gw_ip
);
3290 /* Update bgp route dampening information. */
3291 if (CHECK_FLAG(bgp
->af_flags
[afi
][safi
], BGP_CONFIG_DAMPENING
)
3292 && peer
->sort
== BGP_PEER_EBGP
) {
3293 /* Now we do normal update dampening. */
3294 ret
= bgp_damp_update(pi
, rn
, afi
, safi
);
3295 if (ret
== BGP_DAMP_SUPPRESSED
) {
3296 bgp_unlock_node(rn
);
3301 /* Nexthop reachability check - for unicast and
3302 * labeled-unicast.. */
3303 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
3304 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
)) {
3305 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
3306 && !CHECK_FLAG(peer
->flags
,
3307 PEER_FLAG_DISABLE_CONNECTED_CHECK
)
3309 bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
3314 struct bgp
*bgp_nexthop
= bgp
;
3316 if (pi
->extra
&& pi
->extra
->bgp_orig
)
3317 bgp_nexthop
= pi
->extra
->bgp_orig
;
3319 if (bgp_find_or_add_nexthop(bgp
, bgp_nexthop
, afi
, pi
,
3321 || CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
3322 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_VALID
);
3324 if (BGP_DEBUG(nht
, NHT
)) {
3325 char buf1
[INET6_ADDRSTRLEN
];
3327 (const void *)&attr_new
3329 buf1
, INET6_ADDRSTRLEN
);
3330 zlog_debug("%s(%s): NH unresolved",
3331 __FUNCTION__
, buf1
);
3333 bgp_path_info_unset_flag(rn
, pi
,
3337 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_VALID
);
3340 if (safi
== SAFI_MPLS_VPN
) {
3341 struct bgp_node
*prn
= NULL
;
3342 struct bgp_table
*table
= NULL
;
3344 prn
= bgp_node_get(bgp
->rib
[afi
][safi
],
3345 (struct prefix
*)prd
);
3346 if (bgp_node_has_bgp_path_info_data(prn
)) {
3347 table
= bgp_node_get_bgp_table_info(prn
);
3349 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
3350 bgp
, prd
, table
, p
, pi
);
3352 bgp_unlock_node(prn
);
3356 /* If this is an EVPN route and some attribute has changed,
3358 * route for import. If the extended community has changed, we
3360 * have done the un-import earlier and the import would result
3362 * route getting injected into appropriate L2 VNIs. If it is
3364 * some other attribute change, the import will result in
3366 * the attributes for the route in the VNI(s).
3368 if (safi
== SAFI_EVPN
&& !same_attr
)
3369 bgp_evpn_import_route(bgp
, afi
, safi
, p
, pi
);
3371 /* Process change. */
3372 bgp_aggregate_increment(bgp
, p
, pi
, afi
, safi
);
3374 bgp_process(bgp
, rn
, afi
, safi
);
3375 bgp_unlock_node(rn
);
3377 if (SAFI_UNICAST
== safi
3378 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
3379 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3381 vpn_leak_from_vrf_update(bgp_get_default(), bgp
, pi
);
3383 if ((SAFI_MPLS_VPN
== safi
)
3384 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3386 vpn_leak_to_vrf_update(bgp
, pi
);
3390 if (SAFI_MPLS_VPN
== safi
) {
3391 mpls_label_t label_decoded
= decode_label(label
);
3393 rfapiProcessUpdate(peer
, NULL
, p
, prd
, attr
, afi
, safi
,
3394 type
, sub_type
, &label_decoded
);
3396 if (SAFI_ENCAP
== safi
) {
3397 rfapiProcessUpdate(peer
, NULL
, p
, prd
, attr
, afi
, safi
,
3398 type
, sub_type
, NULL
);
3403 } // End of implicit withdraw
3405 /* Received Logging. */
3406 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3407 if (!peer
->rcvd_attr_printed
) {
3408 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
3409 peer
->rcvd_attr_str
);
3410 peer
->rcvd_attr_printed
= 1;
3413 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
, num_labels
,
3414 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3416 zlog_debug("%s rcvd %s", peer
->host
, pfx_buf
);
3419 /* Make new BGP info. */
3420 new = info_make(type
, sub_type
, 0, peer
, attr_new
, rn
);
3422 /* Update MPLS label */
3423 if (has_valid_label
) {
3424 extra
= bgp_path_info_extra_get(new);
3425 if (extra
->label
!= label
) {
3426 memcpy(&extra
->label
, label
, num_labels
* sizeof(mpls_label_t
));
3427 extra
->num_labels
= num_labels
;
3429 if (!(afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
))
3430 bgp_set_valid_label(&extra
->label
[0]);
3433 /* Update Overlay Index */
3434 if (afi
== AFI_L2VPN
) {
3435 overlay_index_update(new->attr
,
3436 evpn
== NULL
? NULL
: &evpn
->eth_s_id
,
3437 evpn
== NULL
? NULL
: &evpn
->gw_ip
);
3439 /* Nexthop reachability check. */
3440 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
3441 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
)) {
3442 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
3443 && !CHECK_FLAG(peer
->flags
,
3444 PEER_FLAG_DISABLE_CONNECTED_CHECK
)
3445 && !bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
3450 if (bgp_find_or_add_nexthop(bgp
, bgp
, afi
, new, NULL
, connected
)
3451 || CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
3452 bgp_path_info_set_flag(rn
, new, BGP_PATH_VALID
);
3454 if (BGP_DEBUG(nht
, NHT
)) {
3455 char buf1
[INET6_ADDRSTRLEN
];
3457 (const void *)&attr_new
->nexthop
,
3458 buf1
, INET6_ADDRSTRLEN
);
3459 zlog_debug("%s(%s): NH unresolved",
3460 __FUNCTION__
, buf1
);
3462 bgp_path_info_unset_flag(rn
, new, BGP_PATH_VALID
);
3465 bgp_path_info_set_flag(rn
, new, BGP_PATH_VALID
);
3468 new->addpath_rx_id
= addpath_id
;
3470 /* Increment prefix */
3471 bgp_aggregate_increment(bgp
, p
, new, afi
, safi
);
3473 /* Register new BGP information. */
3474 bgp_path_info_add(rn
, new);
3476 /* route_node_get lock */
3477 bgp_unlock_node(rn
);
3480 if (safi
== SAFI_MPLS_VPN
) {
3481 struct bgp_node
*prn
= NULL
;
3482 struct bgp_table
*table
= NULL
;
3484 prn
= bgp_node_get(bgp
->rib
[afi
][safi
], (struct prefix
*)prd
);
3485 if (bgp_node_has_bgp_path_info_data(prn
)) {
3486 table
= bgp_node_get_bgp_table_info(prn
);
3488 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
3489 bgp
, prd
, table
, p
, new);
3491 bgp_unlock_node(prn
);
3495 /* If maximum prefix count is configured and current prefix
3497 if (bgp_maximum_prefix_overflow(peer
, afi
, safi
, 0))
3500 /* If this is an EVPN route, process for import. */
3501 if (safi
== SAFI_EVPN
)
3502 bgp_evpn_import_route(bgp
, afi
, safi
, p
, new);
3504 /* Process change. */
3505 bgp_process(bgp
, rn
, afi
, safi
);
3507 if (SAFI_UNICAST
== safi
3508 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
3509 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3510 vpn_leak_from_vrf_update(bgp_get_default(), bgp
, new);
3512 if ((SAFI_MPLS_VPN
== safi
)
3513 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3515 vpn_leak_to_vrf_update(bgp
, new);
3518 if (SAFI_MPLS_VPN
== safi
) {
3519 mpls_label_t label_decoded
= decode_label(label
);
3521 rfapiProcessUpdate(peer
, NULL
, p
, prd
, attr
, afi
, safi
, type
,
3522 sub_type
, &label_decoded
);
3524 if (SAFI_ENCAP
== safi
) {
3525 rfapiProcessUpdate(peer
, NULL
, p
, prd
, attr
, afi
, safi
, type
,
3532 /* This BGP update is filtered. Log the reason then update BGP
3535 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3536 if (!peer
->rcvd_attr_printed
) {
3537 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
3538 peer
->rcvd_attr_str
);
3539 peer
->rcvd_attr_printed
= 1;
3542 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
, num_labels
,
3543 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3545 zlog_debug("%s rcvd UPDATE about %s -- DENIED due to: %s",
3546 peer
->host
, pfx_buf
, reason
);
3550 /* If this is an EVPN route, un-import it as it is now filtered.
3552 if (safi
== SAFI_EVPN
)
3553 bgp_evpn_unimport_route(bgp
, afi
, safi
, p
, pi
);
3555 if (SAFI_UNICAST
== safi
3556 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
3557 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3559 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp
, pi
);
3561 if ((SAFI_MPLS_VPN
== safi
)
3562 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3564 vpn_leak_to_vrf_withdraw(bgp
, pi
);
3567 bgp_rib_remove(rn
, pi
, peer
, afi
, safi
);
3570 bgp_unlock_node(rn
);
3574 * Filtered update is treated as an implicit withdrawal (see
3576 * a few lines above)
3578 if ((SAFI_MPLS_VPN
== safi
) || (SAFI_ENCAP
== safi
)) {
3579 rfapiProcessWithdraw(peer
, NULL
, p
, prd
, NULL
, afi
, safi
, type
,
3587 int bgp_withdraw(struct peer
*peer
, struct prefix
*p
, uint32_t addpath_id
,
3588 struct attr
*attr
, afi_t afi
, safi_t safi
, int type
,
3589 int sub_type
, struct prefix_rd
*prd
, mpls_label_t
*label
,
3590 uint32_t num_labels
, struct bgp_route_evpn
*evpn
)
3593 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
3594 struct bgp_node
*rn
;
3595 struct bgp_path_info
*pi
;
3598 if ((SAFI_MPLS_VPN
== safi
) || (SAFI_ENCAP
== safi
)) {
3599 rfapiProcessWithdraw(peer
, NULL
, p
, prd
, NULL
, afi
, safi
, type
,
3607 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, prd
);
3609 /* If peer is soft reconfiguration enabled. Record input packet for
3610 * further calculation.
3612 * Cisco IOS 12.4(24)T4 on session establishment sends withdraws for all
3613 * routes that are filtered. This tanks out Quagga RS pretty badly due
3615 * the iteration over all RS clients.
3616 * Since we need to remove the entry from adj_in anyway, do that first
3618 * if there was no entry, we don't need to do anything more.
3620 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SOFT_RECONFIG
)
3621 && peer
!= bgp
->peer_self
)
3622 if (!bgp_adj_in_unset(rn
, peer
, addpath_id
)) {
3623 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3624 bgp_debug_rdpfxpath2str(
3625 afi
, safi
, prd
, p
, label
, num_labels
,
3626 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3629 "%s withdrawing route %s not in adj-in",
3630 peer
->host
, pfx_buf
);
3632 bgp_unlock_node(rn
);
3636 /* Lookup withdrawn route. */
3637 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3638 if (pi
->peer
== peer
&& pi
->type
== type
3639 && pi
->sub_type
== sub_type
3640 && pi
->addpath_rx_id
== addpath_id
)
3644 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3645 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
, num_labels
,
3646 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3648 zlog_debug("%s rcvd UPDATE about %s -- withdrawn", peer
->host
,
3652 /* Withdraw specified route from routing table. */
3653 if (pi
&& !CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
)) {
3654 bgp_rib_withdraw(rn
, pi
, peer
, afi
, safi
, prd
);
3655 if (SAFI_UNICAST
== safi
3656 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
3657 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3658 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp
, pi
);
3660 if ((SAFI_MPLS_VPN
== safi
)
3661 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3663 vpn_leak_to_vrf_withdraw(bgp
, pi
);
3665 } else if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3666 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
, num_labels
,
3667 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3669 zlog_debug("%s Can't find the route %s", peer
->host
, pfx_buf
);
3672 /* Unlock bgp_node_get() lock. */
3673 bgp_unlock_node(rn
);
3678 void bgp_default_originate(struct peer
*peer
, afi_t afi
, safi_t safi
,
3681 struct update_subgroup
*subgrp
;
3682 subgrp
= peer_subgroup(peer
, afi
, safi
);
3683 subgroup_default_originate(subgrp
, withdraw
);
3688 * bgp_stop_announce_route_timer
3690 void bgp_stop_announce_route_timer(struct peer_af
*paf
)
3692 if (!paf
->t_announce_route
)
3695 THREAD_TIMER_OFF(paf
->t_announce_route
);
3699 * bgp_announce_route_timer_expired
3701 * Callback that is invoked when the route announcement timer for a
3704 static int bgp_announce_route_timer_expired(struct thread
*t
)
3706 struct peer_af
*paf
;
3709 paf
= THREAD_ARG(t
);
3712 if (peer
->status
!= Established
)
3715 if (!peer
->afc_nego
[paf
->afi
][paf
->safi
])
3718 peer_af_announce_route(paf
, 1);
3723 * bgp_announce_route
3725 * *Triggers* announcement of routes of a given AFI/SAFI to a peer.
3727 void bgp_announce_route(struct peer
*peer
, afi_t afi
, safi_t safi
)
3729 struct peer_af
*paf
;
3730 struct update_subgroup
*subgrp
;
3732 paf
= peer_af_find(peer
, afi
, safi
);
3735 subgrp
= PAF_SUBGRP(paf
);
3738 * Ignore if subgroup doesn't exist (implies AF is not negotiated)
3739 * or a refresh has already been triggered.
3741 if (!subgrp
|| paf
->t_announce_route
)
3745 * Start a timer to stagger/delay the announce. This serves
3746 * two purposes - announcement can potentially be combined for
3747 * multiple peers and the announcement doesn't happen in the
3750 thread_add_timer_msec(bm
->master
, bgp_announce_route_timer_expired
, paf
,
3751 (subgrp
->peer_count
== 1)
3752 ? BGP_ANNOUNCE_ROUTE_SHORT_DELAY_MS
3753 : BGP_ANNOUNCE_ROUTE_DELAY_MS
,
3754 &paf
->t_announce_route
);
3758 * Announce routes from all AF tables to a peer.
3760 * This should ONLY be called when there is a need to refresh the
3761 * routes to the peer based on a policy change for this peer alone
3762 * or a route refresh request received from the peer.
3763 * The operation will result in splitting the peer from its existing
3764 * subgroups and putting it in new subgroups.
3766 void bgp_announce_route_all(struct peer
*peer
)
3771 FOREACH_AFI_SAFI (afi
, safi
)
3772 bgp_announce_route(peer
, afi
, safi
);
3775 static void bgp_soft_reconfig_table(struct peer
*peer
, afi_t afi
, safi_t safi
,
3776 struct bgp_table
*table
,
3777 struct prefix_rd
*prd
)
3780 struct bgp_node
*rn
;
3781 struct bgp_adj_in
*ain
;
3784 table
= peer
->bgp
->rib
[afi
][safi
];
3786 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
3787 for (ain
= rn
->adj_in
; ain
; ain
= ain
->next
) {
3788 if (ain
->peer
!= peer
)
3791 struct bgp_path_info
*pi
=
3792 bgp_node_get_bgp_path_info(rn
);
3793 uint32_t num_labels
= 0;
3794 mpls_label_t
*label_pnt
= NULL
;
3795 struct bgp_route_evpn evpn
;
3797 if (pi
&& pi
->extra
)
3798 num_labels
= pi
->extra
->num_labels
;
3800 label_pnt
= &pi
->extra
->label
[0];
3802 memcpy(&evpn
, &pi
->attr
->evpn_overlay
,
3805 memset(&evpn
, 0, sizeof(evpn
));
3807 ret
= bgp_update(peer
, &rn
->p
, ain
->addpath_rx_id
,
3808 ain
->attr
, afi
, safi
, ZEBRA_ROUTE_BGP
,
3809 BGP_ROUTE_NORMAL
, prd
, label_pnt
,
3810 num_labels
, 1, &evpn
);
3813 bgp_unlock_node(rn
);
3819 void bgp_soft_reconfig_in(struct peer
*peer
, afi_t afi
, safi_t safi
)
3821 struct bgp_node
*rn
;
3822 struct bgp_table
*table
;
3824 if (peer
->status
!= Established
)
3827 if ((safi
!= SAFI_MPLS_VPN
) && (safi
!= SAFI_ENCAP
)
3828 && (safi
!= SAFI_EVPN
))
3829 bgp_soft_reconfig_table(peer
, afi
, safi
, NULL
, NULL
);
3831 for (rn
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); rn
;
3832 rn
= bgp_route_next(rn
)) {
3833 table
= bgp_node_get_bgp_table_info(rn
);
3834 if (table
!= NULL
) {
3835 struct prefix_rd prd
;
3837 prd
.family
= AF_UNSPEC
;
3839 memcpy(&prd
.val
, rn
->p
.u
.val
, 8);
3841 bgp_soft_reconfig_table(peer
, afi
, safi
, table
,
3848 struct bgp_clear_node_queue
{
3849 struct bgp_node
*rn
;
3852 static wq_item_status
bgp_clear_route_node(struct work_queue
*wq
, void *data
)
3854 struct bgp_clear_node_queue
*cnq
= data
;
3855 struct bgp_node
*rn
= cnq
->rn
;
3856 struct peer
*peer
= wq
->spec
.data
;
3857 struct bgp_path_info
*pi
;
3859 afi_t afi
= bgp_node_table(rn
)->afi
;
3860 safi_t safi
= bgp_node_table(rn
)->safi
;
3865 /* It is possible that we have multiple paths for a prefix from a peer
3866 * if that peer is using AddPath.
3868 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
3869 if (pi
->peer
!= peer
)
3872 /* graceful restart STALE flag set. */
3873 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)
3874 && peer
->nsf
[afi
][safi
]
3875 && !CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
)
3876 && !CHECK_FLAG(pi
->flags
, BGP_PATH_UNUSEABLE
))
3877 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_STALE
);
3879 /* If this is an EVPN route, process for
3881 if (safi
== SAFI_EVPN
)
3882 bgp_evpn_unimport_route(bgp
, afi
, safi
, &rn
->p
,
3884 /* Handle withdraw for VRF route-leaking and L3VPN */
3885 if (SAFI_UNICAST
== safi
3886 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
||
3887 bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3888 vpn_leak_from_vrf_withdraw(bgp_get_default(),
3891 if (SAFI_MPLS_VPN
== safi
&&
3892 bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3893 vpn_leak_to_vrf_withdraw(bgp
, pi
);
3896 bgp_rib_remove(rn
, pi
, peer
, afi
, safi
);
3902 static void bgp_clear_node_queue_del(struct work_queue
*wq
, void *data
)
3904 struct bgp_clear_node_queue
*cnq
= data
;
3905 struct bgp_node
*rn
= cnq
->rn
;
3906 struct bgp_table
*table
= bgp_node_table(rn
);
3908 bgp_unlock_node(rn
);
3909 bgp_table_unlock(table
);
3910 XFREE(MTYPE_BGP_CLEAR_NODE_QUEUE
, cnq
);
3913 static void bgp_clear_node_complete(struct work_queue
*wq
)
3915 struct peer
*peer
= wq
->spec
.data
;
3917 /* Tickle FSM to start moving again */
3918 BGP_EVENT_ADD(peer
, Clearing_Completed
);
3920 peer_unlock(peer
); /* bgp_clear_route */
3923 static void bgp_clear_node_queue_init(struct peer
*peer
)
3925 char wname
[sizeof("clear xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")];
3927 snprintf(wname
, sizeof(wname
), "clear %s", peer
->host
);
3928 #undef CLEAR_QUEUE_NAME_LEN
3930 peer
->clear_node_queue
= work_queue_new(bm
->master
, wname
);
3931 peer
->clear_node_queue
->spec
.hold
= 10;
3932 peer
->clear_node_queue
->spec
.workfunc
= &bgp_clear_route_node
;
3933 peer
->clear_node_queue
->spec
.del_item_data
= &bgp_clear_node_queue_del
;
3934 peer
->clear_node_queue
->spec
.completion_func
= &bgp_clear_node_complete
;
3935 peer
->clear_node_queue
->spec
.max_retries
= 0;
3937 /* we only 'lock' this peer reference when the queue is actually active
3939 peer
->clear_node_queue
->spec
.data
= peer
;
3942 static void bgp_clear_route_table(struct peer
*peer
, afi_t afi
, safi_t safi
,
3943 struct bgp_table
*table
)
3945 struct bgp_node
*rn
;
3946 int force
= bm
->process_main_queue
? 0 : 1;
3949 table
= peer
->bgp
->rib
[afi
][safi
];
3951 /* If still no table => afi/safi isn't configured at all or smth. */
3955 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3956 struct bgp_path_info
*pi
, *next
;
3957 struct bgp_adj_in
*ain
;
3958 struct bgp_adj_in
*ain_next
;
3960 /* XXX:TODO: This is suboptimal, every non-empty route_node is
3961 * queued for every clearing peer, regardless of whether it is
3962 * relevant to the peer at hand.
3964 * Overview: There are 3 different indices which need to be
3965 * scrubbed, potentially, when a peer is removed:
3967 * 1 peer's routes visible via the RIB (ie accepted routes)
3968 * 2 peer's routes visible by the (optional) peer's adj-in index
3969 * 3 other routes visible by the peer's adj-out index
3971 * 3 there is no hurry in scrubbing, once the struct peer is
3972 * removed from bgp->peer, we could just GC such deleted peer's
3973 * adj-outs at our leisure.
3975 * 1 and 2 must be 'scrubbed' in some way, at least made
3976 * invisible via RIB index before peer session is allowed to be
3977 * brought back up. So one needs to know when such a 'search' is
3982 * - there'd be a single global queue or a single RIB walker
3983 * - rather than tracking which route_nodes still need to be
3984 * examined on a peer basis, we'd track which peers still
3987 * Given that our per-peer prefix-counts now should be reliable,
3988 * this may actually be achievable. It doesn't seem to be a huge
3989 * problem at this time,
3991 * It is possible that we have multiple paths for a prefix from
3993 * if that peer is using AddPath.
3997 ain_next
= ain
->next
;
3999 if (ain
->peer
== peer
) {
4000 bgp_adj_in_remove(rn
, ain
);
4001 bgp_unlock_node(rn
);
4007 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= next
) {
4009 if (pi
->peer
!= peer
)
4013 bgp_path_info_reap(rn
, pi
);
4015 struct bgp_clear_node_queue
*cnq
;
4017 /* both unlocked in bgp_clear_node_queue_del */
4018 bgp_table_lock(bgp_node_table(rn
));
4021 MTYPE_BGP_CLEAR_NODE_QUEUE
,
4022 sizeof(struct bgp_clear_node_queue
));
4024 work_queue_add(peer
->clear_node_queue
, cnq
);
4032 void bgp_clear_route(struct peer
*peer
, afi_t afi
, safi_t safi
)
4034 struct bgp_node
*rn
;
4035 struct bgp_table
*table
;
4037 if (peer
->clear_node_queue
== NULL
)
4038 bgp_clear_node_queue_init(peer
);
4040 /* bgp_fsm.c keeps sessions in state Clearing, not transitioning to
4041 * Idle until it receives a Clearing_Completed event. This protects
4042 * against peers which flap faster than we can we clear, which could
4045 * a) race with routes from the new session being installed before
4046 * clear_route_node visits the node (to delete the route of that
4048 * b) resource exhaustion, clear_route_node likely leads to an entry
4049 * on the process_main queue. Fast-flapping could cause that queue
4053 /* lock peer in assumption that clear-node-queue will get nodes; if so,
4054 * the unlock will happen upon work-queue completion; other wise, the
4055 * unlock happens at the end of this function.
4057 if (!peer
->clear_node_queue
->thread
)
4060 if (safi
!= SAFI_MPLS_VPN
&& safi
!= SAFI_ENCAP
&& safi
!= SAFI_EVPN
)
4061 bgp_clear_route_table(peer
, afi
, safi
, NULL
);
4063 for (rn
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); rn
;
4064 rn
= bgp_route_next(rn
)) {
4065 table
= bgp_node_get_bgp_table_info(rn
);
4069 bgp_clear_route_table(peer
, afi
, safi
, table
);
4072 /* unlock if no nodes got added to the clear-node-queue. */
4073 if (!peer
->clear_node_queue
->thread
)
4077 void bgp_clear_route_all(struct peer
*peer
)
4082 FOREACH_AFI_SAFI (afi
, safi
)
4083 bgp_clear_route(peer
, afi
, safi
);
4086 rfapiProcessPeerDown(peer
);
4090 void bgp_clear_adj_in(struct peer
*peer
, afi_t afi
, safi_t safi
)
4092 struct bgp_table
*table
;
4093 struct bgp_node
*rn
;
4094 struct bgp_adj_in
*ain
;
4095 struct bgp_adj_in
*ain_next
;
4097 table
= peer
->bgp
->rib
[afi
][safi
];
4099 /* It is possible that we have multiple paths for a prefix from a peer
4100 * if that peer is using AddPath.
4102 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4106 ain_next
= ain
->next
;
4108 if (ain
->peer
== peer
) {
4109 bgp_adj_in_remove(rn
, ain
);
4110 bgp_unlock_node(rn
);
4118 void bgp_clear_stale_route(struct peer
*peer
, afi_t afi
, safi_t safi
)
4120 struct bgp_node
*rn
;
4121 struct bgp_path_info
*pi
;
4122 struct bgp_table
*table
;
4124 if (safi
== SAFI_MPLS_VPN
) {
4125 for (rn
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); rn
;
4126 rn
= bgp_route_next(rn
)) {
4127 struct bgp_node
*rm
;
4129 /* look for neighbor in tables */
4130 table
= bgp_node_get_bgp_table_info(rn
);
4134 for (rm
= bgp_table_top(table
); rm
;
4135 rm
= bgp_route_next(rm
))
4136 for (pi
= bgp_node_get_bgp_path_info(rm
); pi
;
4138 if (pi
->peer
!= peer
)
4140 if (!CHECK_FLAG(pi
->flags
,
4144 bgp_rib_remove(rm
, pi
, peer
, afi
, safi
);
4149 for (rn
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); rn
;
4150 rn
= bgp_route_next(rn
))
4151 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
4153 if (pi
->peer
!= peer
)
4155 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
))
4157 bgp_rib_remove(rn
, pi
, peer
, afi
, safi
);
4163 static void bgp_cleanup_table(struct bgp
*bgp
, struct bgp_table
*table
,
4166 struct bgp_node
*rn
;
4167 struct bgp_path_info
*pi
;
4168 struct bgp_path_info
*next
;
4170 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
4171 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= next
) {
4173 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4174 && pi
->type
== ZEBRA_ROUTE_BGP
4175 && (pi
->sub_type
== BGP_ROUTE_NORMAL
4176 || pi
->sub_type
== BGP_ROUTE_AGGREGATE
4177 || pi
->sub_type
== BGP_ROUTE_IMPORTED
)) {
4179 if (bgp_fibupd_safi(safi
))
4180 bgp_zebra_withdraw(&rn
->p
, pi
, bgp
,
4182 bgp_path_info_reap(rn
, pi
);
4187 /* Delete all kernel routes. */
4188 void bgp_cleanup_routes(struct bgp
*bgp
)
4191 struct bgp_node
*rn
;
4192 struct bgp_table
*table
;
4194 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4195 if (afi
== AFI_L2VPN
)
4197 bgp_cleanup_table(bgp
, bgp
->rib
[afi
][SAFI_UNICAST
],
4200 * VPN and ENCAP and EVPN tables are two-level (RD is top level)
4202 if (afi
!= AFI_L2VPN
) {
4204 safi
= SAFI_MPLS_VPN
;
4205 for (rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn
;
4206 rn
= bgp_route_next(rn
)) {
4207 table
= bgp_node_get_bgp_table_info(rn
);
4208 if (table
!= NULL
) {
4209 bgp_cleanup_table(bgp
, table
, safi
);
4210 bgp_table_finish(&table
);
4211 bgp_node_set_bgp_table_info(rn
, NULL
);
4212 bgp_unlock_node(rn
);
4216 for (rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn
;
4217 rn
= bgp_route_next(rn
)) {
4218 table
= bgp_node_get_bgp_table_info(rn
);
4219 if (table
!= NULL
) {
4220 bgp_cleanup_table(bgp
, table
, safi
);
4221 bgp_table_finish(&table
);
4222 bgp_node_set_bgp_table_info(rn
, NULL
);
4223 bgp_unlock_node(rn
);
4228 for (rn
= bgp_table_top(bgp
->rib
[AFI_L2VPN
][SAFI_EVPN
]); rn
;
4229 rn
= bgp_route_next(rn
)) {
4230 table
= bgp_node_get_bgp_table_info(rn
);
4231 if (table
!= NULL
) {
4232 bgp_cleanup_table(bgp
, table
, SAFI_EVPN
);
4233 bgp_table_finish(&table
);
4234 bgp_node_set_bgp_table_info(rn
, NULL
);
4235 bgp_unlock_node(rn
);
4240 void bgp_reset(void)
4243 bgp_zclient_reset();
4244 access_list_reset();
4245 prefix_list_reset();
4248 static int bgp_addpath_encode_rx(struct peer
*peer
, afi_t afi
, safi_t safi
)
4250 return (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4251 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4252 PEER_CAP_ADDPATH_AF_TX_RCV
));
4255 /* Parse NLRI stream. Withdraw NLRI is recognized by NULL attr
4257 int bgp_nlri_parse_ip(struct peer
*peer
, struct attr
*attr
,
4258 struct bgp_nlri
*packet
)
4267 int addpath_encoded
;
4268 uint32_t addpath_id
;
4271 lim
= pnt
+ packet
->length
;
4273 safi
= packet
->safi
;
4275 addpath_encoded
= bgp_addpath_encode_rx(peer
, afi
, safi
);
4277 /* RFC4771 6.3 The NLRI field in the UPDATE message is checked for
4278 syntactic validity. If the field is syntactically incorrect,
4279 then the Error Subcode is set to Invalid Network Field. */
4280 for (; pnt
< lim
; pnt
+= psize
) {
4281 /* Clear prefix structure. */
4282 memset(&p
, 0, sizeof(struct prefix
));
4284 if (addpath_encoded
) {
4286 /* When packet overflow occurs return immediately. */
4287 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4290 addpath_id
= ntohl(*((uint32_t *)pnt
));
4291 pnt
+= BGP_ADDPATH_ID_LEN
;
4294 /* Fetch prefix length. */
4295 p
.prefixlen
= *pnt
++;
4296 /* afi/safi validity already verified by caller,
4297 * bgp_update_receive */
4298 p
.family
= afi2family(afi
);
4300 /* Prefix length check. */
4301 if (p
.prefixlen
> prefix_blen(&p
) * 8) {
4304 "%s [Error] Update packet error (wrong prefix length %d for afi %u)",
4305 peer
->host
, p
.prefixlen
, packet
->afi
);
4309 /* Packet size overflow check. */
4310 psize
= PSIZE(p
.prefixlen
);
4312 /* When packet overflow occur return immediately. */
4313 if (pnt
+ psize
> lim
) {
4316 "%s [Error] Update packet error (prefix length %d overflows packet)",
4317 peer
->host
, p
.prefixlen
);
4321 /* Defensive coding, double-check the psize fits in a struct
4323 if (psize
> (ssize_t
)sizeof(p
.u
)) {
4326 "%s [Error] Update packet error (prefix length %d too large for prefix storage %zu)",
4327 peer
->host
, p
.prefixlen
, sizeof(p
.u
));
4331 /* Fetch prefix from NLRI packet. */
4332 memcpy(p
.u
.val
, pnt
, psize
);
4334 /* Check address. */
4335 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
4336 if (IN_CLASSD(ntohl(p
.u
.prefix4
.s_addr
))) {
4337 /* From RFC4271 Section 6.3:
4339 * If a prefix in the NLRI field is semantically
4341 * (e.g., an unexpected multicast IP address),
4343 * be logged locally, and the prefix SHOULD be
4348 "%s: IPv4 unicast NLRI is multicast address %s, ignoring",
4349 peer
->host
, inet_ntoa(p
.u
.prefix4
));
4354 /* Check address. */
4355 if (afi
== AFI_IP6
&& safi
== SAFI_UNICAST
) {
4356 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
)) {
4361 "%s: IPv6 unicast NLRI is link-local address %s, ignoring",
4363 inet_ntop(AF_INET6
, &p
.u
.prefix6
, buf
,
4368 if (IN6_IS_ADDR_MULTICAST(&p
.u
.prefix6
)) {
4373 "%s: IPv6 unicast NLRI is multicast address %s, ignoring",
4375 inet_ntop(AF_INET6
, &p
.u
.prefix6
, buf
,
4382 /* Normal process. */
4384 ret
= bgp_update(peer
, &p
, addpath_id
, attr
, afi
, safi
,
4385 ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4386 NULL
, NULL
, 0, 0, NULL
);
4388 ret
= bgp_withdraw(peer
, &p
, addpath_id
, attr
, afi
,
4389 safi
, ZEBRA_ROUTE_BGP
,
4390 BGP_ROUTE_NORMAL
, NULL
, NULL
, 0,
4393 /* Address family configuration mismatch or maximum-prefix count
4399 /* Packet length consistency check. */
4403 "%s [Error] Update packet error (prefix length mismatch with total length)",
4411 static struct bgp_static
*bgp_static_new(void)
4413 return XCALLOC(MTYPE_BGP_STATIC
, sizeof(struct bgp_static
));
4416 static void bgp_static_free(struct bgp_static
*bgp_static
)
4418 if (bgp_static
->rmap
.name
)
4419 XFREE(MTYPE_ROUTE_MAP_NAME
, bgp_static
->rmap
.name
);
4420 if (bgp_static
->eth_s_id
)
4421 XFREE(MTYPE_ATTR
, bgp_static
->eth_s_id
);
4422 XFREE(MTYPE_BGP_STATIC
, bgp_static
);
4425 void bgp_static_update(struct bgp
*bgp
, struct prefix
*p
,
4426 struct bgp_static
*bgp_static
, afi_t afi
, safi_t safi
)
4428 struct bgp_node
*rn
;
4429 struct bgp_path_info
*pi
;
4430 struct bgp_path_info
*new;
4431 struct bgp_path_info rmap_path
;
4433 struct attr
*attr_new
;
4436 int vnc_implicit_withdraw
= 0;
4443 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, NULL
);
4445 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
4447 attr
.nexthop
= bgp_static
->igpnexthop
;
4448 attr
.med
= bgp_static
->igpmetric
;
4449 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
4451 if (bgp_static
->atomic
)
4452 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
4454 /* Store label index, if required. */
4455 if (bgp_static
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
4456 attr
.label_index
= bgp_static
->label_index
;
4457 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
);
4460 /* Apply route-map. */
4461 if (bgp_static
->rmap
.name
) {
4462 struct attr attr_tmp
= attr
;
4464 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
4465 rmap_path
.peer
= bgp
->peer_self
;
4466 rmap_path
.attr
= &attr_tmp
;
4468 SET_FLAG(bgp
->peer_self
->rmap_type
, PEER_RMAP_TYPE_NETWORK
);
4470 ret
= route_map_apply(bgp_static
->rmap
.map
, p
, RMAP_BGP
,
4473 bgp
->peer_self
->rmap_type
= 0;
4475 if (ret
== RMAP_DENYMATCH
) {
4476 /* Free uninterned attribute. */
4477 bgp_attr_flush(&attr_tmp
);
4479 /* Unintern original. */
4480 aspath_unintern(&attr
.aspath
);
4481 bgp_static_withdraw(bgp
, p
, afi
, safi
);
4485 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
))
4486 bgp_attr_add_gshut_community(&attr_tmp
);
4488 attr_new
= bgp_attr_intern(&attr_tmp
);
4491 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
))
4492 bgp_attr_add_gshut_community(&attr
);
4494 attr_new
= bgp_attr_intern(&attr
);
4497 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
4498 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
4499 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4503 if (attrhash_cmp(pi
->attr
, attr_new
)
4504 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)
4505 && !bgp_flag_check(bgp
, BGP_FLAG_FORCE_STATIC_PROCESS
)) {
4506 bgp_unlock_node(rn
);
4507 bgp_attr_unintern(&attr_new
);
4508 aspath_unintern(&attr
.aspath
);
4511 /* The attribute is changed. */
4512 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
4514 /* Rewrite BGP route information. */
4515 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
4516 bgp_path_info_restore(rn
, pi
);
4518 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
4520 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
4521 && (safi
== SAFI_UNICAST
)) {
4522 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
4524 * Implicit withdraw case.
4525 * We have to do this before pi is
4528 ++vnc_implicit_withdraw
;
4529 vnc_import_bgp_del_route(bgp
, p
, pi
);
4530 vnc_import_bgp_exterior_del_route(
4535 bgp_attr_unintern(&pi
->attr
);
4536 pi
->attr
= attr_new
;
4537 pi
->uptime
= bgp_clock();
4539 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
4540 && (safi
== SAFI_UNICAST
)) {
4541 if (vnc_implicit_withdraw
) {
4542 vnc_import_bgp_add_route(bgp
, p
, pi
);
4543 vnc_import_bgp_exterior_add_route(
4549 /* Nexthop reachability check. */
4550 if (bgp_flag_check(bgp
, BGP_FLAG_IMPORT_CHECK
)
4551 && (safi
== SAFI_UNICAST
4552 || safi
== SAFI_LABELED_UNICAST
)) {
4554 struct bgp
*bgp_nexthop
= bgp
;
4556 if (pi
->extra
&& pi
->extra
->bgp_orig
)
4557 bgp_nexthop
= pi
->extra
->bgp_orig
;
4559 if (bgp_find_or_add_nexthop(bgp
, bgp_nexthop
,
4561 bgp_path_info_set_flag(rn
, pi
,
4564 if (BGP_DEBUG(nht
, NHT
)) {
4565 char buf1
[INET6_ADDRSTRLEN
];
4566 inet_ntop(p
->family
,
4570 "%s(%s): Route not in table, not advertising",
4571 __FUNCTION__
, buf1
);
4573 bgp_path_info_unset_flag(
4574 rn
, pi
, BGP_PATH_VALID
);
4577 /* Delete the NHT structure if any, if we're
4579 * enabling/disabling import check. We
4580 * deregister the route
4581 * from NHT to avoid overloading NHT and the
4582 * process interaction
4584 bgp_unlink_nexthop(pi
);
4585 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_VALID
);
4587 /* Process change. */
4588 bgp_aggregate_increment(bgp
, p
, pi
, afi
, safi
);
4589 bgp_process(bgp
, rn
, afi
, safi
);
4591 if (SAFI_UNICAST
== safi
4592 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
4594 == BGP_INSTANCE_TYPE_DEFAULT
)) {
4595 vpn_leak_from_vrf_update(bgp_get_default(), bgp
,
4599 bgp_unlock_node(rn
);
4600 aspath_unintern(&attr
.aspath
);
4605 /* Make new BGP info. */
4606 new = info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0, bgp
->peer_self
,
4608 /* Nexthop reachability check. */
4609 if (bgp_flag_check(bgp
, BGP_FLAG_IMPORT_CHECK
)
4610 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
)) {
4611 if (bgp_find_or_add_nexthop(bgp
, bgp
, afi
, new, NULL
, 0))
4612 bgp_path_info_set_flag(rn
, new, BGP_PATH_VALID
);
4614 if (BGP_DEBUG(nht
, NHT
)) {
4615 char buf1
[INET6_ADDRSTRLEN
];
4616 inet_ntop(p
->family
, &p
->u
.prefix
, buf1
,
4619 "%s(%s): Route not in table, not advertising",
4620 __FUNCTION__
, buf1
);
4622 bgp_path_info_unset_flag(rn
, new, BGP_PATH_VALID
);
4625 /* Delete the NHT structure if any, if we're toggling between
4626 * enabling/disabling import check. We deregister the route
4627 * from NHT to avoid overloading NHT and the process interaction
4629 bgp_unlink_nexthop(new);
4631 bgp_path_info_set_flag(rn
, new, BGP_PATH_VALID
);
4634 /* Aggregate address increment. */
4635 bgp_aggregate_increment(bgp
, p
, new, afi
, safi
);
4637 /* Register new BGP information. */
4638 bgp_path_info_add(rn
, new);
4640 /* route_node_get lock */
4641 bgp_unlock_node(rn
);
4643 /* Process change. */
4644 bgp_process(bgp
, rn
, afi
, safi
);
4646 if (SAFI_UNICAST
== safi
4647 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
4648 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
4649 vpn_leak_from_vrf_update(bgp_get_default(), bgp
, new);
4652 /* Unintern original. */
4653 aspath_unintern(&attr
.aspath
);
4656 void bgp_static_withdraw(struct bgp
*bgp
, struct prefix
*p
, afi_t afi
,
4659 struct bgp_node
*rn
;
4660 struct bgp_path_info
*pi
;
4662 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, NULL
);
4664 /* Check selected route and self inserted route. */
4665 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
4666 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
4667 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4670 /* Withdraw static BGP route from routing table. */
4672 if (SAFI_UNICAST
== safi
4673 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
4674 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
4675 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp
, pi
);
4677 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
4678 bgp_unlink_nexthop(pi
);
4679 bgp_path_info_delete(rn
, pi
);
4680 bgp_process(bgp
, rn
, afi
, safi
);
4683 /* Unlock bgp_node_lookup. */
4684 bgp_unlock_node(rn
);
4688 * Used for SAFI_MPLS_VPN and SAFI_ENCAP
4690 static void bgp_static_withdraw_safi(struct bgp
*bgp
, struct prefix
*p
,
4691 afi_t afi
, safi_t safi
,
4692 struct prefix_rd
*prd
)
4694 struct bgp_node
*rn
;
4695 struct bgp_path_info
*pi
;
4697 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, prd
);
4699 /* Check selected route and self inserted route. */
4700 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
4701 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
4702 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4705 /* Withdraw static BGP route from routing table. */
4708 rfapiProcessWithdraw(
4709 pi
->peer
, NULL
, p
, prd
, pi
->attr
, afi
, safi
, pi
->type
,
4710 1); /* Kill, since it is an administrative change */
4712 if (SAFI_MPLS_VPN
== safi
4713 && bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4714 vpn_leak_to_vrf_withdraw(bgp
, pi
);
4716 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
4717 bgp_path_info_delete(rn
, pi
);
4718 bgp_process(bgp
, rn
, afi
, safi
);
4721 /* Unlock bgp_node_lookup. */
4722 bgp_unlock_node(rn
);
4725 static void bgp_static_update_safi(struct bgp
*bgp
, struct prefix
*p
,
4726 struct bgp_static
*bgp_static
, afi_t afi
,
4729 struct bgp_node
*rn
;
4730 struct bgp_path_info
*new;
4731 struct attr
*attr_new
;
4732 struct attr attr
= {0};
4733 struct bgp_path_info
*pi
;
4735 mpls_label_t label
= 0;
4737 uint32_t num_labels
= 0;
4742 if (bgp_static
->label
!= MPLS_INVALID_LABEL
)
4744 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
,
4747 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
4749 attr
.nexthop
= bgp_static
->igpnexthop
;
4750 attr
.med
= bgp_static
->igpmetric
;
4751 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
4753 if ((safi
== SAFI_EVPN
) || (safi
== SAFI_MPLS_VPN
)
4754 || (safi
== SAFI_ENCAP
)) {
4755 if (afi
== AFI_IP
) {
4756 attr
.mp_nexthop_global_in
= bgp_static
->igpnexthop
;
4757 attr
.mp_nexthop_len
= IPV4_MAX_BYTELEN
;
4760 if (afi
== AFI_L2VPN
) {
4761 if (bgp_static
->gatewayIp
.family
== AF_INET
)
4763 bgp_static
->gatewayIp
.u
.prefix4
.s_addr
;
4764 else if (bgp_static
->gatewayIp
.family
== AF_INET6
)
4765 memcpy(&(add
.ipv6
), &(bgp_static
->gatewayIp
.u
.prefix6
),
4766 sizeof(struct in6_addr
));
4767 overlay_index_update(&attr
, bgp_static
->eth_s_id
, &add
);
4768 if (bgp_static
->encap_tunneltype
== BGP_ENCAP_TYPE_VXLAN
) {
4769 struct bgp_encap_type_vxlan bet
;
4770 memset(&bet
, 0, sizeof(struct bgp_encap_type_vxlan
));
4771 bet
.vnid
= p
->u
.prefix_evpn
.prefix_addr
.eth_tag
;
4772 bgp_encap_type_vxlan_to_tlv(&bet
, &attr
);
4774 if (bgp_static
->router_mac
) {
4775 bgp_add_routermac_ecom(&attr
, bgp_static
->router_mac
);
4778 /* Apply route-map. */
4779 if (bgp_static
->rmap
.name
) {
4780 struct attr attr_tmp
= attr
;
4781 struct bgp_path_info rmap_path
;
4784 rmap_path
.peer
= bgp
->peer_self
;
4785 rmap_path
.attr
= &attr_tmp
;
4787 SET_FLAG(bgp
->peer_self
->rmap_type
, PEER_RMAP_TYPE_NETWORK
);
4789 ret
= route_map_apply(bgp_static
->rmap
.map
, p
, RMAP_BGP
,
4792 bgp
->peer_self
->rmap_type
= 0;
4794 if (ret
== RMAP_DENYMATCH
) {
4795 /* Free uninterned attribute. */
4796 bgp_attr_flush(&attr_tmp
);
4798 /* Unintern original. */
4799 aspath_unintern(&attr
.aspath
);
4800 bgp_static_withdraw_safi(bgp
, p
, afi
, safi
,
4805 attr_new
= bgp_attr_intern(&attr_tmp
);
4807 attr_new
= bgp_attr_intern(&attr
);
4810 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
4811 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
4812 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4816 memset(&add
, 0, sizeof(union gw_addr
));
4817 if (attrhash_cmp(pi
->attr
, attr_new
)
4818 && overlay_index_equal(afi
, pi
, bgp_static
->eth_s_id
, &add
)
4819 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
4820 bgp_unlock_node(rn
);
4821 bgp_attr_unintern(&attr_new
);
4822 aspath_unintern(&attr
.aspath
);
4825 /* The attribute is changed. */
4826 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
4828 /* Rewrite BGP route information. */
4829 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
4830 bgp_path_info_restore(rn
, pi
);
4832 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
4833 bgp_attr_unintern(&pi
->attr
);
4834 pi
->attr
= attr_new
;
4835 pi
->uptime
= bgp_clock();
4838 label
= decode_label(&pi
->extra
->label
[0]);
4841 /* Process change. */
4842 bgp_aggregate_increment(bgp
, p
, pi
, afi
, safi
);
4843 bgp_process(bgp
, rn
, afi
, safi
);
4845 if (SAFI_MPLS_VPN
== safi
4846 && bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4847 vpn_leak_to_vrf_update(bgp
, pi
);
4850 rfapiProcessUpdate(pi
->peer
, NULL
, p
, &bgp_static
->prd
,
4851 pi
->attr
, afi
, safi
, pi
->type
,
4852 pi
->sub_type
, &label
);
4854 bgp_unlock_node(rn
);
4855 aspath_unintern(&attr
.aspath
);
4861 /* Make new BGP info. */
4862 new = info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0, bgp
->peer_self
,
4864 SET_FLAG(new->flags
, BGP_PATH_VALID
);
4865 new->extra
= bgp_path_info_extra_new();
4867 new->extra
->label
[0] = bgp_static
->label
;
4868 new->extra
->num_labels
= num_labels
;
4871 label
= decode_label(&bgp_static
->label
);
4874 /* Aggregate address increment. */
4875 bgp_aggregate_increment(bgp
, p
, new, afi
, safi
);
4877 /* Register new BGP information. */
4878 bgp_path_info_add(rn
, new);
4879 /* route_node_get lock */
4880 bgp_unlock_node(rn
);
4882 /* Process change. */
4883 bgp_process(bgp
, rn
, afi
, safi
);
4885 if (SAFI_MPLS_VPN
== safi
4886 && bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4887 vpn_leak_to_vrf_update(bgp
, new);
4890 rfapiProcessUpdate(new->peer
, NULL
, p
, &bgp_static
->prd
, new->attr
, afi
,
4891 safi
, new->type
, new->sub_type
, &label
);
4894 /* Unintern original. */
4895 aspath_unintern(&attr
.aspath
);
4898 /* Configure static BGP network. When user don't run zebra, static
4899 route should be installed as valid. */
4900 static int bgp_static_set(struct vty
*vty
, const char *negate
,
4901 const char *ip_str
, afi_t afi
, safi_t safi
,
4902 const char *rmap
, int backdoor
, uint32_t label_index
)
4904 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
4907 struct bgp_static
*bgp_static
;
4908 struct bgp_node
*rn
;
4909 uint8_t need_update
= 0;
4911 /* Convert IP prefix string to struct prefix. */
4912 ret
= str2prefix(ip_str
, &p
);
4914 vty_out(vty
, "%% Malformed prefix\n");
4915 return CMD_WARNING_CONFIG_FAILED
;
4917 if (afi
== AFI_IP6
&& IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
)) {
4918 vty_out(vty
, "%% Malformed prefix (link-local address)\n");
4919 return CMD_WARNING_CONFIG_FAILED
;
4926 /* Set BGP static route configuration. */
4927 rn
= bgp_node_lookup(bgp
->route
[afi
][safi
], &p
);
4930 vty_out(vty
, "%% Can't find static route specified\n");
4931 return CMD_WARNING_CONFIG_FAILED
;
4934 bgp_static
= bgp_node_get_bgp_static_info(rn
);
4936 if ((label_index
!= BGP_INVALID_LABEL_INDEX
)
4937 && (label_index
!= bgp_static
->label_index
)) {
4939 "%% label-index doesn't match static route\n");
4940 return CMD_WARNING_CONFIG_FAILED
;
4943 if ((rmap
&& bgp_static
->rmap
.name
)
4944 && strcmp(rmap
, bgp_static
->rmap
.name
)) {
4946 "%% route-map name doesn't match static route\n");
4947 return CMD_WARNING_CONFIG_FAILED
;
4950 /* Update BGP RIB. */
4951 if (!bgp_static
->backdoor
)
4952 bgp_static_withdraw(bgp
, &p
, afi
, safi
);
4954 /* Clear configuration. */
4955 bgp_static_free(bgp_static
);
4956 bgp_node_set_bgp_static_info(rn
, NULL
);
4957 bgp_unlock_node(rn
);
4958 bgp_unlock_node(rn
);
4961 /* Set BGP static route configuration. */
4962 rn
= bgp_node_get(bgp
->route
[afi
][safi
], &p
);
4964 bgp_static
= bgp_node_get_bgp_static_info(rn
);
4966 /* Configuration change. */
4967 /* Label index cannot be changed. */
4968 if (bgp_static
->label_index
!= label_index
) {
4969 vty_out(vty
, "%% cannot change label-index\n");
4970 return CMD_WARNING_CONFIG_FAILED
;
4973 /* Check previous routes are installed into BGP. */
4974 if (bgp_static
->valid
4975 && bgp_static
->backdoor
!= backdoor
)
4978 bgp_static
->backdoor
= backdoor
;
4981 if (bgp_static
->rmap
.name
)
4982 XFREE(MTYPE_ROUTE_MAP_NAME
,
4983 bgp_static
->rmap
.name
);
4984 bgp_static
->rmap
.name
=
4985 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
4986 bgp_static
->rmap
.map
=
4987 route_map_lookup_by_name(rmap
);
4989 if (bgp_static
->rmap
.name
)
4990 XFREE(MTYPE_ROUTE_MAP_NAME
,
4991 bgp_static
->rmap
.name
);
4992 bgp_static
->rmap
.name
= NULL
;
4993 bgp_static
->rmap
.map
= NULL
;
4994 bgp_static
->valid
= 0;
4996 bgp_unlock_node(rn
);
4998 /* New configuration. */
4999 bgp_static
= bgp_static_new();
5000 bgp_static
->backdoor
= backdoor
;
5001 bgp_static
->valid
= 0;
5002 bgp_static
->igpmetric
= 0;
5003 bgp_static
->igpnexthop
.s_addr
= 0;
5004 bgp_static
->label_index
= label_index
;
5007 if (bgp_static
->rmap
.name
)
5008 XFREE(MTYPE_ROUTE_MAP_NAME
,
5009 bgp_static
->rmap
.name
);
5010 bgp_static
->rmap
.name
=
5011 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5012 bgp_static
->rmap
.map
=
5013 route_map_lookup_by_name(rmap
);
5015 bgp_node_set_bgp_static_info(rn
, bgp_static
);
5018 bgp_static
->valid
= 1;
5020 bgp_static_withdraw(bgp
, &p
, afi
, safi
);
5022 if (!bgp_static
->backdoor
)
5023 bgp_static_update(bgp
, &p
, bgp_static
, afi
, safi
);
5029 void bgp_static_add(struct bgp
*bgp
)
5033 struct bgp_node
*rn
;
5034 struct bgp_node
*rm
;
5035 struct bgp_table
*table
;
5036 struct bgp_static
*bgp_static
;
5038 FOREACH_AFI_SAFI (afi
, safi
)
5039 for (rn
= bgp_table_top(bgp
->route
[afi
][safi
]); rn
;
5040 rn
= bgp_route_next(rn
)) {
5041 if (!bgp_node_has_bgp_path_info_data(rn
))
5044 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
5045 || (safi
== SAFI_EVPN
)) {
5046 table
= bgp_node_get_bgp_table_info(rn
);
5048 for (rm
= bgp_table_top(table
); rm
;
5049 rm
= bgp_route_next(rm
)) {
5051 bgp_node_get_bgp_static_info(
5053 bgp_static_update_safi(bgp
, &rm
->p
,
5060 bgp_node_get_bgp_static_info(rn
), afi
,
5066 /* Called from bgp_delete(). Delete all static routes from the BGP
5068 void bgp_static_delete(struct bgp
*bgp
)
5072 struct bgp_node
*rn
;
5073 struct bgp_node
*rm
;
5074 struct bgp_table
*table
;
5075 struct bgp_static
*bgp_static
;
5077 FOREACH_AFI_SAFI (afi
, safi
)
5078 for (rn
= bgp_table_top(bgp
->route
[afi
][safi
]); rn
;
5079 rn
= bgp_route_next(rn
)) {
5080 if (!bgp_node_has_bgp_path_info_data(rn
))
5083 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
5084 || (safi
== SAFI_EVPN
)) {
5085 table
= bgp_node_get_bgp_table_info(rn
);
5087 for (rm
= bgp_table_top(table
); rm
;
5088 rm
= bgp_route_next(rm
)) {
5090 bgp_node_get_bgp_static_info(
5095 bgp_static_withdraw_safi(
5096 bgp
, &rm
->p
, AFI_IP
, safi
,
5097 (struct prefix_rd
*)&rn
->p
);
5098 bgp_static_free(bgp_static
);
5099 bgp_node_set_bgp_static_info(rn
, NULL
);
5100 bgp_unlock_node(rn
);
5103 bgp_static
= bgp_node_get_bgp_static_info(rn
);
5104 bgp_static_withdraw(bgp
, &rn
->p
, afi
, safi
);
5105 bgp_static_free(bgp_static
);
5106 bgp_node_set_bgp_static_info(rn
, NULL
);
5107 bgp_unlock_node(rn
);
5112 void bgp_static_redo_import_check(struct bgp
*bgp
)
5116 struct bgp_node
*rn
;
5117 struct bgp_node
*rm
;
5118 struct bgp_table
*table
;
5119 struct bgp_static
*bgp_static
;
5121 /* Use this flag to force reprocessing of the route */
5122 bgp_flag_set(bgp
, BGP_FLAG_FORCE_STATIC_PROCESS
);
5123 FOREACH_AFI_SAFI (afi
, safi
) {
5124 for (rn
= bgp_table_top(bgp
->route
[afi
][safi
]); rn
;
5125 rn
= bgp_route_next(rn
)) {
5126 if (!bgp_node_has_bgp_path_info_data(rn
))
5129 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
5130 || (safi
== SAFI_EVPN
)) {
5131 table
= bgp_node_get_bgp_table_info(rn
);
5133 for (rm
= bgp_table_top(table
); rm
;
5134 rm
= bgp_route_next(rm
)) {
5136 bgp_node_get_bgp_static_info(
5138 bgp_static_update_safi(bgp
, &rm
->p
,
5143 bgp_static
= bgp_node_get_bgp_static_info(rn
);
5144 bgp_static_update(bgp
, &rn
->p
, bgp_static
, afi
,
5149 bgp_flag_unset(bgp
, BGP_FLAG_FORCE_STATIC_PROCESS
);
5152 static void bgp_purge_af_static_redist_routes(struct bgp
*bgp
, afi_t afi
,
5155 struct bgp_table
*table
;
5156 struct bgp_node
*rn
;
5157 struct bgp_path_info
*pi
;
5159 table
= bgp
->rib
[afi
][safi
];
5160 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5161 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
5162 if (pi
->peer
== bgp
->peer_self
5163 && ((pi
->type
== ZEBRA_ROUTE_BGP
5164 && pi
->sub_type
== BGP_ROUTE_STATIC
)
5165 || (pi
->type
!= ZEBRA_ROUTE_BGP
5167 == BGP_ROUTE_REDISTRIBUTE
))) {
5168 bgp_aggregate_decrement(bgp
, &rn
->p
, pi
, afi
,
5170 bgp_unlink_nexthop(pi
);
5171 bgp_path_info_delete(rn
, pi
);
5172 bgp_process(bgp
, rn
, afi
, safi
);
5179 * Purge all networks and redistributed routes from routing table.
5180 * Invoked upon the instance going down.
5182 void bgp_purge_static_redist_routes(struct bgp
*bgp
)
5187 FOREACH_AFI_SAFI (afi
, safi
)
5188 bgp_purge_af_static_redist_routes(bgp
, afi
, safi
);
5193 * Currently this is used to set static routes for VPN and ENCAP.
5194 * I think it can probably be factored with bgp_static_set.
5196 int bgp_static_set_safi(afi_t afi
, safi_t safi
, struct vty
*vty
,
5197 const char *ip_str
, const char *rd_str
,
5198 const char *label_str
, const char *rmap_str
,
5199 int evpn_type
, const char *esi
, const char *gwip
,
5200 const char *ethtag
, const char *routermac
)
5202 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5205 struct prefix_rd prd
;
5206 struct bgp_node
*prn
;
5207 struct bgp_node
*rn
;
5208 struct bgp_table
*table
;
5209 struct bgp_static
*bgp_static
;
5210 mpls_label_t label
= MPLS_INVALID_LABEL
;
5211 struct prefix gw_ip
;
5213 /* validate ip prefix */
5214 ret
= str2prefix(ip_str
, &p
);
5216 vty_out(vty
, "%% Malformed prefix\n");
5217 return CMD_WARNING_CONFIG_FAILED
;
5220 if ((afi
== AFI_L2VPN
)
5221 && (bgp_build_evpn_prefix(evpn_type
,
5222 ethtag
!= NULL
? atol(ethtag
) : 0, &p
))) {
5223 vty_out(vty
, "%% L2VPN prefix could not be forged\n");
5224 return CMD_WARNING_CONFIG_FAILED
;
5227 ret
= str2prefix_rd(rd_str
, &prd
);
5229 vty_out(vty
, "%% Malformed rd\n");
5230 return CMD_WARNING_CONFIG_FAILED
;
5234 unsigned long label_val
;
5235 label_val
= strtoul(label_str
, NULL
, 10);
5236 encode_label(label_val
, &label
);
5239 if (safi
== SAFI_EVPN
) {
5240 if (esi
&& str2esi(esi
, NULL
) == 0) {
5241 vty_out(vty
, "%% Malformed ESI\n");
5242 return CMD_WARNING_CONFIG_FAILED
;
5244 if (routermac
&& prefix_str2mac(routermac
, NULL
) == 0) {
5245 vty_out(vty
, "%% Malformed Router MAC\n");
5246 return CMD_WARNING_CONFIG_FAILED
;
5249 memset(&gw_ip
, 0, sizeof(struct prefix
));
5250 ret
= str2prefix(gwip
, &gw_ip
);
5252 vty_out(vty
, "%% Malformed GatewayIp\n");
5253 return CMD_WARNING_CONFIG_FAILED
;
5255 if ((gw_ip
.family
== AF_INET
5256 && is_evpn_prefix_ipaddr_v6(
5257 (struct prefix_evpn
*)&p
))
5258 || (gw_ip
.family
== AF_INET6
5259 && is_evpn_prefix_ipaddr_v4(
5260 (struct prefix_evpn
*)&p
))) {
5262 "%% GatewayIp family differs with IP prefix\n");
5263 return CMD_WARNING_CONFIG_FAILED
;
5267 prn
= bgp_node_get(bgp
->route
[afi
][safi
], (struct prefix
*)&prd
);
5268 if (!bgp_node_has_bgp_path_info_data(prn
))
5269 bgp_node_set_bgp_table_info(prn
,
5270 bgp_table_init(bgp
, afi
, safi
));
5271 table
= bgp_node_get_bgp_table_info(prn
);
5273 rn
= bgp_node_get(table
, &p
);
5275 if (bgp_node_has_bgp_path_info_data(rn
)) {
5276 vty_out(vty
, "%% Same network configuration exists\n");
5277 bgp_unlock_node(rn
);
5279 /* New configuration. */
5280 bgp_static
= bgp_static_new();
5281 bgp_static
->backdoor
= 0;
5282 bgp_static
->valid
= 0;
5283 bgp_static
->igpmetric
= 0;
5284 bgp_static
->igpnexthop
.s_addr
= 0;
5285 bgp_static
->label
= label
;
5286 bgp_static
->prd
= prd
;
5289 if (bgp_static
->rmap
.name
)
5290 XFREE(MTYPE_ROUTE_MAP_NAME
,
5291 bgp_static
->rmap
.name
);
5292 bgp_static
->rmap
.name
=
5293 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap_str
);
5294 bgp_static
->rmap
.map
=
5295 route_map_lookup_by_name(rmap_str
);
5298 if (safi
== SAFI_EVPN
) {
5300 bgp_static
->eth_s_id
=
5302 sizeof(struct eth_segment_id
));
5303 str2esi(esi
, bgp_static
->eth_s_id
);
5306 bgp_static
->router_mac
=
5307 XCALLOC(MTYPE_ATTR
, ETH_ALEN
+ 1);
5308 (void)prefix_str2mac(routermac
,
5309 bgp_static
->router_mac
);
5312 prefix_copy(&bgp_static
->gatewayIp
, &gw_ip
);
5314 bgp_node_set_bgp_static_info(rn
, bgp_static
);
5316 bgp_static
->valid
= 1;
5317 bgp_static_update_safi(bgp
, &p
, bgp_static
, afi
, safi
);
5323 /* Configure static BGP network. */
5324 int bgp_static_unset_safi(afi_t afi
, safi_t safi
, struct vty
*vty
,
5325 const char *ip_str
, const char *rd_str
,
5326 const char *label_str
, int evpn_type
, const char *esi
,
5327 const char *gwip
, const char *ethtag
)
5329 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5332 struct prefix_rd prd
;
5333 struct bgp_node
*prn
;
5334 struct bgp_node
*rn
;
5335 struct bgp_table
*table
;
5336 struct bgp_static
*bgp_static
;
5337 mpls_label_t label
= MPLS_INVALID_LABEL
;
5339 /* Convert IP prefix string to struct prefix. */
5340 ret
= str2prefix(ip_str
, &p
);
5342 vty_out(vty
, "%% Malformed prefix\n");
5343 return CMD_WARNING_CONFIG_FAILED
;
5346 if ((afi
== AFI_L2VPN
)
5347 && (bgp_build_evpn_prefix(evpn_type
,
5348 ethtag
!= NULL
? atol(ethtag
) : 0, &p
))) {
5349 vty_out(vty
, "%% L2VPN prefix could not be forged\n");
5350 return CMD_WARNING_CONFIG_FAILED
;
5352 ret
= str2prefix_rd(rd_str
, &prd
);
5354 vty_out(vty
, "%% Malformed rd\n");
5355 return CMD_WARNING_CONFIG_FAILED
;
5359 unsigned long label_val
;
5360 label_val
= strtoul(label_str
, NULL
, 10);
5361 encode_label(label_val
, &label
);
5364 prn
= bgp_node_get(bgp
->route
[afi
][safi
], (struct prefix
*)&prd
);
5365 if (!bgp_node_has_bgp_path_info_data(prn
))
5366 bgp_node_set_bgp_table_info(prn
,
5367 bgp_table_init(bgp
, afi
, safi
));
5369 bgp_unlock_node(prn
);
5370 table
= bgp_node_get_bgp_table_info(prn
);
5372 rn
= bgp_node_lookup(table
, &p
);
5375 bgp_static_withdraw_safi(bgp
, &p
, afi
, safi
, &prd
);
5377 bgp_static
= bgp_node_get_bgp_static_info(rn
);
5378 bgp_static_free(bgp_static
);
5379 bgp_node_set_bgp_static_info(rn
, NULL
);
5380 bgp_unlock_node(rn
);
5381 bgp_unlock_node(rn
);
5383 vty_out(vty
, "%% Can't find the route\n");
5388 static int bgp_table_map_set(struct vty
*vty
, afi_t afi
, safi_t safi
,
5389 const char *rmap_name
)
5391 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5392 struct bgp_rmap
*rmap
;
5394 rmap
= &bgp
->table_map
[afi
][safi
];
5397 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
5398 rmap
->name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap_name
);
5399 rmap
->map
= route_map_lookup_by_name(rmap_name
);
5402 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
5407 if (bgp_fibupd_safi(safi
))
5408 bgp_zebra_announce_table(bgp
, afi
, safi
);
5413 static int bgp_table_map_unset(struct vty
*vty
, afi_t afi
, safi_t safi
,
5414 const char *rmap_name
)
5416 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5417 struct bgp_rmap
*rmap
;
5419 rmap
= &bgp
->table_map
[afi
][safi
];
5421 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
5425 if (bgp_fibupd_safi(safi
))
5426 bgp_zebra_announce_table(bgp
, afi
, safi
);
5431 void bgp_config_write_table_map(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
5434 if (bgp
->table_map
[afi
][safi
].name
) {
5435 vty_out(vty
, " table-map %s\n",
5436 bgp
->table_map
[afi
][safi
].name
);
5440 DEFUN (bgp_table_map
,
5443 "BGP table to RIB route download filter\n"
5444 "Name of the route map\n")
5447 return bgp_table_map_set(vty
, bgp_node_afi(vty
), bgp_node_safi(vty
),
5448 argv
[idx_word
]->arg
);
5450 DEFUN (no_bgp_table_map
,
5451 no_bgp_table_map_cmd
,
5452 "no table-map WORD",
5454 "BGP table to RIB route download filter\n"
5455 "Name of the route map\n")
5458 return bgp_table_map_unset(vty
, bgp_node_afi(vty
), bgp_node_safi(vty
),
5459 argv
[idx_word
]->arg
);
5465 <A.B.C.D/M$prefix|A.B.C.D$address [mask A.B.C.D$netmask]> \
5466 [{route-map WORD$map_name|label-index (0-1048560)$label_index| \
5467 backdoor$backdoor}]",
5469 "Specify a network to announce via BGP\n"
5474 "Route-map to modify the attributes\n"
5475 "Name of the route map\n"
5476 "Label index to associate with the prefix\n"
5477 "Label index value\n"
5478 "Specify a BGP backdoor route\n")
5480 char addr_prefix_str
[BUFSIZ
];
5485 ret
= netmask_str2prefix_str(address_str
, netmask_str
,
5488 vty_out(vty
, "%% Inconsistent address and mask\n");
5489 return CMD_WARNING_CONFIG_FAILED
;
5493 return bgp_static_set(
5494 vty
, no
, address_str
? addr_prefix_str
: prefix_str
, AFI_IP
,
5495 bgp_node_safi(vty
), map_name
, backdoor
? 1 : 0,
5496 label_index
? (uint32_t)label_index
: BGP_INVALID_LABEL_INDEX
);
5499 DEFPY(ipv6_bgp_network
,
5500 ipv6_bgp_network_cmd
,
5501 "[no] network X:X::X:X/M$prefix \
5502 [{route-map WORD$map_name|label-index (0-1048560)$label_index}]",
5504 "Specify a network to announce via BGP\n"
5506 "Route-map to modify the attributes\n"
5507 "Name of the route map\n"
5508 "Label index to associate with the prefix\n"
5509 "Label index value\n")
5511 return bgp_static_set(
5512 vty
, no
, prefix_str
, AFI_IP6
, bgp_node_safi(vty
), map_name
, 0,
5513 label_index
? (uint32_t)label_index
: BGP_INVALID_LABEL_INDEX
);
5516 static struct bgp_aggregate
*bgp_aggregate_new(void)
5518 return XCALLOC(MTYPE_BGP_AGGREGATE
, sizeof(struct bgp_aggregate
));
5521 static void bgp_aggregate_free(struct bgp_aggregate
*aggregate
)
5523 XFREE(MTYPE_BGP_AGGREGATE
, aggregate
);
5526 static int bgp_aggregate_info_same(struct bgp_path_info
*pi
, uint8_t origin
,
5527 struct aspath
*aspath
,
5528 struct community
*comm
,
5529 struct ecommunity
*ecomm
,
5530 struct lcommunity
*lcomm
)
5532 static struct aspath
*ae
= NULL
;
5535 ae
= aspath_empty();
5540 if (origin
!= pi
->attr
->origin
)
5543 if (!aspath_cmp(pi
->attr
->aspath
, (aspath
) ? aspath
: ae
))
5546 if (!community_cmp(pi
->attr
->community
, comm
))
5549 if (!ecommunity_cmp(pi
->attr
->ecommunity
, ecomm
))
5552 if (!lcommunity_cmp(pi
->attr
->lcommunity
, lcomm
))
5555 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
5561 static void bgp_aggregate_install(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5562 struct prefix
*p
, uint8_t origin
,
5563 struct aspath
*aspath
,
5564 struct community
*community
,
5565 struct ecommunity
*ecommunity
,
5566 struct lcommunity
*lcommunity
,
5567 uint8_t atomic_aggregate
,
5568 struct bgp_aggregate
*aggregate
)
5570 struct bgp_node
*rn
;
5571 struct bgp_table
*table
;
5572 struct bgp_path_info
*pi
, *orig
, *new;
5574 table
= bgp
->rib
[afi
][safi
];
5576 rn
= bgp_node_get(table
, p
);
5578 for (orig
= pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
5579 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
5580 && pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5583 if (aggregate
->count
> 0) {
5585 * If the aggregate information has not changed
5586 * no need to re-install it again.
5588 if (bgp_aggregate_info_same(orig
, origin
, aspath
, community
,
5589 ecommunity
, lcommunity
)) {
5590 bgp_unlock_node(rn
);
5593 aspath_free(aspath
);
5595 community_free(&community
);
5597 ecommunity_free(&ecommunity
);
5599 lcommunity_free(&lcommunity
);
5605 * Mark the old as unusable
5608 bgp_path_info_delete(rn
, pi
);
5610 new = info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_AGGREGATE
, 0,
5612 bgp_attr_aggregate_intern(bgp
, origin
, aspath
,
5613 community
, ecommunity
,
5618 SET_FLAG(new->flags
, BGP_PATH_VALID
);
5620 bgp_path_info_add(rn
, new);
5621 bgp_process(bgp
, rn
, afi
, safi
);
5623 for (pi
= orig
; pi
; pi
= pi
->next
)
5624 if (pi
->peer
== bgp
->peer_self
5625 && pi
->type
== ZEBRA_ROUTE_BGP
5626 && pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5629 /* Withdraw static BGP route from routing table. */
5631 bgp_path_info_delete(rn
, pi
);
5632 bgp_process(bgp
, rn
, afi
, safi
);
5636 bgp_unlock_node(rn
);
5639 /* Update an aggregate as routes are added/removed from the BGP table */
5640 static void bgp_aggregate_route(struct bgp
*bgp
, struct prefix
*p
,
5641 afi_t afi
, safi_t safi
,
5642 struct bgp_aggregate
*aggregate
)
5644 struct bgp_table
*table
;
5645 struct bgp_node
*top
;
5646 struct bgp_node
*rn
;
5648 struct aspath
*aspath
= NULL
;
5649 struct community
*community
= NULL
;
5650 struct ecommunity
*ecommunity
= NULL
;
5651 struct lcommunity
*lcommunity
= NULL
;
5652 struct bgp_path_info
*pi
;
5653 unsigned long match
= 0;
5654 uint8_t atomic_aggregate
= 0;
5656 /* ORIGIN attribute: If at least one route among routes that are
5657 aggregated has ORIGIN with the value INCOMPLETE, then the
5658 aggregated route must have the ORIGIN attribute with the value
5659 INCOMPLETE. Otherwise, if at least one route among routes that
5660 are aggregated has ORIGIN with the value EGP, then the aggregated
5661 route must have the origin attribute with the value EGP. In all
5662 other case the value of the ORIGIN attribute of the aggregated
5663 route is INTERNAL. */
5664 origin
= BGP_ORIGIN_IGP
;
5666 table
= bgp
->rib
[afi
][safi
];
5668 top
= bgp_node_get(table
, p
);
5669 for (rn
= bgp_node_get(table
, p
); rn
;
5670 rn
= bgp_route_next_until(rn
, top
)) {
5671 if (rn
->p
.prefixlen
<= p
->prefixlen
)
5676 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
5677 if (BGP_PATH_HOLDDOWN(pi
))
5681 & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
))
5682 atomic_aggregate
= 1;
5684 if (pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5688 * summary-only aggregate route suppress
5689 * aggregated route announcements.
5691 if (aggregate
->summary_only
) {
5692 (bgp_path_info_extra_get(pi
))->suppress
++;
5693 bgp_path_info_set_flag(rn
, pi
,
5694 BGP_PATH_ATTR_CHANGED
);
5701 * If at least one route among routes that are
5702 * aggregated has ORIGIN with the value INCOMPLETE,
5703 * then the aggregated route MUST have the ORIGIN
5704 * attribute with the value INCOMPLETE. Otherwise, if
5705 * at least one route among routes that are aggregated
5706 * has ORIGIN with the value EGP, then the aggregated
5707 * route MUST have the ORIGIN attribute with the value
5710 switch (pi
->attr
->origin
) {
5711 case BGP_ORIGIN_INCOMPLETE
:
5712 aggregate
->incomplete_origin_count
++;
5714 case BGP_ORIGIN_EGP
:
5715 aggregate
->egp_origin_count
++;
5723 if (!aggregate
->as_set
)
5727 * as-set aggregate route generate origin, as path,
5728 * and community aggregation.
5730 /* Compute aggregate route's as-path.
5732 bgp_compute_aggregate_aspath(aggregate
,
5735 /* Compute aggregate route's community.
5737 if (pi
->attr
->community
)
5738 bgp_compute_aggregate_community(
5740 pi
->attr
->community
);
5742 /* Compute aggregate route's extended community.
5744 if (pi
->attr
->ecommunity
)
5745 bgp_compute_aggregate_ecommunity(
5747 pi
->attr
->ecommunity
);
5749 /* Compute aggregate route's large community.
5751 if (pi
->attr
->lcommunity
)
5752 bgp_compute_aggregate_lcommunity(
5754 pi
->attr
->lcommunity
);
5757 bgp_process(bgp
, rn
, afi
, safi
);
5759 bgp_unlock_node(top
);
5762 if (aggregate
->incomplete_origin_count
> 0)
5763 origin
= BGP_ORIGIN_INCOMPLETE
;
5764 else if (aggregate
->egp_origin_count
> 0)
5765 origin
= BGP_ORIGIN_EGP
;
5767 if (aggregate
->as_set
) {
5768 if (aggregate
->aspath
)
5769 /* Retrieve aggregate route's as-path.
5771 aspath
= aspath_dup(aggregate
->aspath
);
5773 if (aggregate
->community
)
5774 /* Retrieve aggregate route's community.
5776 community
= community_dup(aggregate
->community
);
5778 if (aggregate
->ecommunity
)
5779 /* Retrieve aggregate route's ecommunity.
5781 ecommunity
= ecommunity_dup(aggregate
->ecommunity
);
5783 if (aggregate
->lcommunity
)
5784 /* Retrieve aggregate route's lcommunity.
5786 lcommunity
= lcommunity_dup(aggregate
->lcommunity
);
5789 bgp_aggregate_install(bgp
, afi
, safi
, p
, origin
, aspath
, community
,
5790 ecommunity
, lcommunity
, atomic_aggregate
,
5794 static void bgp_aggregate_delete(struct bgp
*bgp
, struct prefix
*p
, afi_t afi
,
5795 safi_t safi
, struct bgp_aggregate
*aggregate
)
5797 struct bgp_table
*table
;
5798 struct bgp_node
*top
;
5799 struct bgp_node
*rn
;
5800 struct bgp_path_info
*pi
;
5801 unsigned long match
;
5803 table
= bgp
->rib
[afi
][safi
];
5805 /* If routes exists below this node, generate aggregate routes. */
5806 top
= bgp_node_get(table
, p
);
5807 for (rn
= bgp_node_get(table
, p
); rn
;
5808 rn
= bgp_route_next_until(rn
, top
)) {
5809 if (rn
->p
.prefixlen
<= p
->prefixlen
)
5813 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
5814 if (BGP_PATH_HOLDDOWN(pi
))
5817 if (pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5820 if (aggregate
->summary_only
&& pi
->extra
) {
5821 pi
->extra
->suppress
--;
5823 if (pi
->extra
->suppress
== 0) {
5824 bgp_path_info_set_flag(
5825 rn
, pi
, BGP_PATH_ATTR_CHANGED
);
5831 if (pi
->attr
->origin
== BGP_ORIGIN_INCOMPLETE
)
5832 aggregate
->incomplete_origin_count
--;
5833 else if (pi
->attr
->origin
== BGP_ORIGIN_EGP
)
5834 aggregate
->egp_origin_count
--;
5836 if (aggregate
->as_set
) {
5837 /* Remove as-path from aggregate.
5839 bgp_remove_aspath_from_aggregate(
5843 if (pi
->attr
->community
)
5844 /* Remove community from aggregate.
5846 bgp_remove_community_from_aggregate(
5848 pi
->attr
->community
);
5850 if (pi
->attr
->ecommunity
)
5851 /* Remove ecommunity from aggregate.
5853 bgp_remove_ecommunity_from_aggregate(
5855 pi
->attr
->ecommunity
);
5857 if (pi
->attr
->lcommunity
)
5858 /* Remove lcommunity from aggregate.
5860 bgp_remove_lcommunity_from_aggregate(
5862 pi
->attr
->lcommunity
);
5867 /* If this node was suppressed, process the change. */
5869 bgp_process(bgp
, rn
, afi
, safi
);
5871 bgp_unlock_node(top
);
5874 static void bgp_add_route_to_aggregate(struct bgp
*bgp
, struct prefix
*aggr_p
,
5875 struct bgp_path_info
*pinew
, afi_t afi
,
5877 struct bgp_aggregate
*aggregate
)
5880 struct aspath
*aspath
= NULL
;
5881 uint8_t atomic_aggregate
= 0;
5882 struct community
*community
= NULL
;
5883 struct ecommunity
*ecommunity
= NULL
;
5884 struct lcommunity
*lcommunity
= NULL
;
5886 /* ORIGIN attribute: If at least one route among routes that are
5887 * aggregated has ORIGIN with the value INCOMPLETE, then the
5888 * aggregated route must have the ORIGIN attribute with the value
5889 * INCOMPLETE. Otherwise, if at least one route among routes that
5890 * are aggregated has ORIGIN with the value EGP, then the aggregated
5891 * route must have the origin attribute with the value EGP. In all
5892 * other case the value of the ORIGIN attribute of the aggregated
5893 * route is INTERNAL.
5895 origin
= BGP_ORIGIN_IGP
;
5899 if (aggregate
->summary_only
)
5900 (bgp_path_info_extra_get(pinew
))->suppress
++;
5902 switch (pinew
->attr
->origin
) {
5903 case BGP_ORIGIN_INCOMPLETE
:
5904 aggregate
->incomplete_origin_count
++;
5906 case BGP_ORIGIN_EGP
:
5907 aggregate
->egp_origin_count
++;
5915 if (aggregate
->incomplete_origin_count
> 0)
5916 origin
= BGP_ORIGIN_INCOMPLETE
;
5917 else if (aggregate
->egp_origin_count
> 0)
5918 origin
= BGP_ORIGIN_EGP
;
5920 if (aggregate
->as_set
) {
5921 /* Compute aggregate route's as-path.
5923 bgp_compute_aggregate_aspath(aggregate
,
5924 pinew
->attr
->aspath
);
5926 /* Compute aggregate route's community.
5928 if (pinew
->attr
->community
)
5929 bgp_compute_aggregate_community(
5931 pinew
->attr
->community
);
5933 /* Compute aggregate route's extended community.
5935 if (pinew
->attr
->ecommunity
)
5936 bgp_compute_aggregate_ecommunity(
5938 pinew
->attr
->ecommunity
);
5940 /* Compute aggregate route's large community.
5942 if (pinew
->attr
->lcommunity
)
5943 bgp_compute_aggregate_lcommunity(
5945 pinew
->attr
->lcommunity
);
5947 /* Retrieve aggregate route's as-path.
5949 if (aggregate
->aspath
)
5950 aspath
= aspath_dup(aggregate
->aspath
);
5952 /* Retrieve aggregate route's community.
5954 if (aggregate
->community
)
5955 community
= community_dup(aggregate
->community
);
5957 /* Retrieve aggregate route's ecommunity.
5959 if (aggregate
->ecommunity
)
5960 ecommunity
= ecommunity_dup(aggregate
->ecommunity
);
5962 /* Retrieve aggregate route's lcommunity.
5964 if (aggregate
->lcommunity
)
5965 lcommunity
= lcommunity_dup(aggregate
->lcommunity
);
5968 bgp_aggregate_install(bgp
, afi
, safi
, aggr_p
, origin
,
5969 aspath
, community
, ecommunity
,
5970 lcommunity
, atomic_aggregate
, aggregate
);
5973 static void bgp_remove_route_from_aggregate(struct bgp
*bgp
, afi_t afi
,
5975 struct bgp_path_info
*pi
,
5976 struct bgp_aggregate
*aggregate
,
5977 struct prefix
*aggr_p
)
5980 struct aspath
*aspath
= NULL
;
5981 uint8_t atomic_aggregate
= 0;
5982 struct community
*community
= NULL
;
5983 struct ecommunity
*ecommunity
= NULL
;
5984 struct lcommunity
*lcommunity
= NULL
;
5985 unsigned long match
= 0;
5987 if (BGP_PATH_HOLDDOWN(pi
))
5990 if (pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5993 if (aggregate
->summary_only
5995 && pi
->extra
->suppress
> 0) {
5996 pi
->extra
->suppress
--;
5998 if (pi
->extra
->suppress
== 0) {
5999 bgp_path_info_set_flag(pi
->net
, pi
,
6000 BGP_PATH_ATTR_CHANGED
);
6005 if (aggregate
->count
> 0)
6008 if (pi
->attr
->origin
== BGP_ORIGIN_INCOMPLETE
)
6009 aggregate
->incomplete_origin_count
--;
6010 else if (pi
->attr
->origin
== BGP_ORIGIN_EGP
)
6011 aggregate
->egp_origin_count
--;
6013 if (aggregate
->as_set
) {
6014 /* Remove as-path from aggregate.
6016 bgp_remove_aspath_from_aggregate(aggregate
,
6019 if (pi
->attr
->community
)
6020 /* Remove community from aggregate.
6022 bgp_remove_community_from_aggregate(
6024 pi
->attr
->community
);
6026 if (pi
->attr
->ecommunity
)
6027 /* Remove ecommunity from aggregate.
6029 bgp_remove_ecommunity_from_aggregate(
6031 pi
->attr
->ecommunity
);
6033 if (pi
->attr
->lcommunity
)
6034 /* Remove lcommunity from aggregate.
6036 bgp_remove_lcommunity_from_aggregate(
6038 pi
->attr
->lcommunity
);
6041 /* If this node was suppressed, process the change. */
6043 bgp_process(bgp
, pi
->net
, afi
, safi
);
6045 origin
= BGP_ORIGIN_IGP
;
6046 if (aggregate
->incomplete_origin_count
> 0)
6047 origin
= BGP_ORIGIN_INCOMPLETE
;
6048 else if (aggregate
->egp_origin_count
> 0)
6049 origin
= BGP_ORIGIN_EGP
;
6051 if (aggregate
->as_set
) {
6052 /* Retrieve aggregate route's as-path.
6054 if (aggregate
->aspath
)
6055 aspath
= aspath_dup(aggregate
->aspath
);
6057 /* Retrieve aggregate route's community.
6059 if (aggregate
->community
)
6060 community
= community_dup(aggregate
->community
);
6062 /* Retrieve aggregate route's ecommunity.
6064 if (aggregate
->ecommunity
)
6065 ecommunity
= ecommunity_dup(aggregate
->ecommunity
);
6067 /* Retrieve aggregate route's lcommunity.
6069 if (aggregate
->lcommunity
)
6070 lcommunity
= lcommunity_dup(aggregate
->lcommunity
);
6073 bgp_aggregate_install(bgp
, afi
, safi
, aggr_p
, origin
,
6074 aspath
, community
, ecommunity
,
6075 lcommunity
, atomic_aggregate
, aggregate
);
6078 void bgp_aggregate_increment(struct bgp
*bgp
, struct prefix
*p
,
6079 struct bgp_path_info
*pi
, afi_t afi
, safi_t safi
)
6081 struct bgp_node
*child
;
6082 struct bgp_node
*rn
;
6083 struct bgp_aggregate
*aggregate
;
6084 struct bgp_table
*table
;
6086 table
= bgp
->aggregate
[afi
][safi
];
6088 /* No aggregates configured. */
6089 if (bgp_table_top_nolock(table
) == NULL
)
6092 if (p
->prefixlen
== 0)
6095 if (BGP_PATH_HOLDDOWN(pi
))
6098 child
= bgp_node_get(table
, p
);
6100 /* Aggregate address configuration check. */
6101 for (rn
= child
; rn
; rn
= bgp_node_parent_nolock(rn
)) {
6102 aggregate
= bgp_node_get_bgp_aggregate_info(rn
);
6103 if (aggregate
!= NULL
&& rn
->p
.prefixlen
< p
->prefixlen
) {
6104 bgp_add_route_to_aggregate(bgp
, &rn
->p
, pi
, afi
,
6108 bgp_unlock_node(child
);
6111 void bgp_aggregate_decrement(struct bgp
*bgp
, struct prefix
*p
,
6112 struct bgp_path_info
*del
, afi_t afi
, safi_t safi
)
6114 struct bgp_node
*child
;
6115 struct bgp_node
*rn
;
6116 struct bgp_aggregate
*aggregate
;
6117 struct bgp_table
*table
;
6119 table
= bgp
->aggregate
[afi
][safi
];
6121 /* No aggregates configured. */
6122 if (bgp_table_top_nolock(table
) == NULL
)
6125 if (p
->prefixlen
== 0)
6128 child
= bgp_node_get(table
, p
);
6130 /* Aggregate address configuration check. */
6131 for (rn
= child
; rn
; rn
= bgp_node_parent_nolock(rn
)) {
6132 aggregate
= bgp_node_get_bgp_aggregate_info(rn
);
6133 if (aggregate
!= NULL
&& rn
->p
.prefixlen
< p
->prefixlen
) {
6134 bgp_remove_route_from_aggregate(bgp
, afi
, safi
,
6135 del
, aggregate
, &rn
->p
);
6138 bgp_unlock_node(child
);
6141 /* Aggregate route attribute. */
6142 #define AGGREGATE_SUMMARY_ONLY 1
6143 #define AGGREGATE_AS_SET 1
6145 static int bgp_aggregate_unset(struct vty
*vty
, const char *prefix_str
,
6146 afi_t afi
, safi_t safi
)
6148 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
6151 struct bgp_node
*rn
;
6152 struct bgp_aggregate
*aggregate
;
6154 /* Convert string to prefix structure. */
6155 ret
= str2prefix(prefix_str
, &p
);
6157 vty_out(vty
, "Malformed prefix\n");
6158 return CMD_WARNING_CONFIG_FAILED
;
6162 /* Old configuration check. */
6163 rn
= bgp_node_lookup(bgp
->aggregate
[afi
][safi
], &p
);
6166 "%% There is no aggregate-address configuration.\n");
6167 return CMD_WARNING_CONFIG_FAILED
;
6170 aggregate
= bgp_node_get_bgp_aggregate_info(rn
);
6171 bgp_aggregate_delete(bgp
, &p
, afi
, safi
, aggregate
);
6172 bgp_aggregate_install(bgp
, afi
, safi
, &p
, 0, NULL
, NULL
,
6173 NULL
, NULL
, 0, aggregate
);
6175 /* Unlock aggregate address configuration. */
6176 bgp_node_set_bgp_aggregate_info(rn
, NULL
);
6178 if (aggregate
->community
)
6179 community_free(&aggregate
->community
);
6181 if (aggregate
->community_hash
) {
6182 /* Delete all communities in the hash.
6184 hash_clean(aggregate
->community_hash
,
6185 bgp_aggr_community_remove
);
6186 /* Free up the community_hash.
6188 hash_free(aggregate
->community_hash
);
6191 if (aggregate
->ecommunity
)
6192 ecommunity_free(&aggregate
->ecommunity
);
6194 if (aggregate
->ecommunity_hash
) {
6195 /* Delete all ecommunities in the hash.
6197 hash_clean(aggregate
->ecommunity_hash
,
6198 bgp_aggr_ecommunity_remove
);
6199 /* Free up the ecommunity_hash.
6201 hash_free(aggregate
->ecommunity_hash
);
6204 if (aggregate
->lcommunity
)
6205 lcommunity_free(&aggregate
->lcommunity
);
6207 if (aggregate
->lcommunity_hash
) {
6208 /* Delete all lcommunities in the hash.
6210 hash_clean(aggregate
->lcommunity_hash
,
6211 bgp_aggr_lcommunity_remove
);
6212 /* Free up the lcommunity_hash.
6214 hash_free(aggregate
->lcommunity_hash
);
6217 if (aggregate
->aspath
)
6218 aspath_free(aggregate
->aspath
);
6220 if (aggregate
->aspath_hash
) {
6221 /* Delete all as-paths in the hash.
6223 hash_clean(aggregate
->aspath_hash
,
6224 bgp_aggr_aspath_remove
);
6225 /* Free up the aspath_hash.
6227 hash_free(aggregate
->aspath_hash
);
6230 bgp_aggregate_free(aggregate
);
6231 bgp_unlock_node(rn
);
6232 bgp_unlock_node(rn
);
6237 static int bgp_aggregate_set(struct vty
*vty
, const char *prefix_str
, afi_t afi
,
6238 safi_t safi
, uint8_t summary_only
, uint8_t as_set
)
6240 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
6243 struct bgp_node
*rn
;
6244 struct bgp_aggregate
*aggregate
;
6246 /* Convert string to prefix structure. */
6247 ret
= str2prefix(prefix_str
, &p
);
6249 vty_out(vty
, "Malformed prefix\n");
6250 return CMD_WARNING_CONFIG_FAILED
;
6254 if ((afi
== AFI_IP
&& p
.prefixlen
== IPV4_MAX_BITLEN
) ||
6255 (afi
== AFI_IP6
&& p
.prefixlen
== IPV6_MAX_BITLEN
)) {
6256 vty_out(vty
, "Specified prefix: %s will not result in any useful aggregation, disallowing\n",
6258 return CMD_WARNING_CONFIG_FAILED
;
6261 /* Old configuration check. */
6262 rn
= bgp_node_get(bgp
->aggregate
[afi
][safi
], &p
);
6264 if (bgp_node_has_bgp_path_info_data(rn
)) {
6265 vty_out(vty
, "There is already same aggregate network.\n");
6266 /* try to remove the old entry */
6267 ret
= bgp_aggregate_unset(vty
, prefix_str
, afi
, safi
);
6269 vty_out(vty
, "Error deleting aggregate.\n");
6270 bgp_unlock_node(rn
);
6271 return CMD_WARNING_CONFIG_FAILED
;
6275 /* Make aggregate address structure. */
6276 aggregate
= bgp_aggregate_new();
6277 aggregate
->summary_only
= summary_only
;
6278 aggregate
->as_set
= as_set
;
6279 aggregate
->safi
= safi
;
6280 bgp_node_set_bgp_aggregate_info(rn
, aggregate
);
6282 /* Aggregate address insert into BGP routing table. */
6283 bgp_aggregate_route(bgp
, &p
, afi
, safi
, aggregate
);
6288 DEFUN (aggregate_address
,
6289 aggregate_address_cmd
,
6290 "aggregate-address A.B.C.D/M [<as-set [summary-only]|summary-only [as-set]>]",
6291 "Configure BGP aggregate entries\n"
6292 "Aggregate prefix\n"
6293 "Generate AS set path information\n"
6294 "Filter more specific routes from updates\n"
6295 "Filter more specific routes from updates\n"
6296 "Generate AS set path information\n")
6299 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
6300 char *prefix
= argv
[idx
]->arg
;
6302 argv_find(argv
, argc
, "as-set", &idx
) ? AGGREGATE_AS_SET
: 0;
6304 int summary_only
= argv_find(argv
, argc
, "summary-only", &idx
)
6305 ? AGGREGATE_SUMMARY_ONLY
6308 return bgp_aggregate_set(vty
, prefix
, AFI_IP
, bgp_node_safi(vty
),
6309 summary_only
, as_set
);
6312 DEFUN (aggregate_address_mask
,
6313 aggregate_address_mask_cmd
,
6314 "aggregate-address A.B.C.D A.B.C.D [<as-set [summary-only]|summary-only [as-set]>]",
6315 "Configure BGP aggregate entries\n"
6316 "Aggregate address\n"
6318 "Generate AS set path information\n"
6319 "Filter more specific routes from updates\n"
6320 "Filter more specific routes from updates\n"
6321 "Generate AS set path information\n")
6324 argv_find(argv
, argc
, "A.B.C.D", &idx
);
6325 char *prefix
= argv
[idx
]->arg
;
6326 char *mask
= argv
[idx
+ 1]->arg
;
6328 argv_find(argv
, argc
, "as-set", &idx
) ? AGGREGATE_AS_SET
: 0;
6330 int summary_only
= argv_find(argv
, argc
, "summary-only", &idx
)
6331 ? AGGREGATE_SUMMARY_ONLY
6334 char prefix_str
[BUFSIZ
];
6335 int ret
= netmask_str2prefix_str(prefix
, mask
, prefix_str
);
6338 vty_out(vty
, "%% Inconsistent address and mask\n");
6339 return CMD_WARNING_CONFIG_FAILED
;
6342 return bgp_aggregate_set(vty
, prefix_str
, AFI_IP
, bgp_node_safi(vty
),
6343 summary_only
, as_set
);
6346 DEFUN (no_aggregate_address
,
6347 no_aggregate_address_cmd
,
6348 "no aggregate-address A.B.C.D/M [<as-set [summary-only]|summary-only [as-set]>]",
6350 "Configure BGP aggregate entries\n"
6351 "Aggregate prefix\n"
6352 "Generate AS set path information\n"
6353 "Filter more specific routes from updates\n"
6354 "Filter more specific routes from updates\n"
6355 "Generate AS set path information\n")
6358 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
6359 char *prefix
= argv
[idx
]->arg
;
6360 return bgp_aggregate_unset(vty
, prefix
, AFI_IP
, bgp_node_safi(vty
));
6363 DEFUN (no_aggregate_address_mask
,
6364 no_aggregate_address_mask_cmd
,
6365 "no aggregate-address A.B.C.D A.B.C.D [<as-set [summary-only]|summary-only [as-set]>]",
6367 "Configure BGP aggregate entries\n"
6368 "Aggregate address\n"
6370 "Generate AS set path information\n"
6371 "Filter more specific routes from updates\n"
6372 "Filter more specific routes from updates\n"
6373 "Generate AS set path information\n")
6376 argv_find(argv
, argc
, "A.B.C.D", &idx
);
6377 char *prefix
= argv
[idx
]->arg
;
6378 char *mask
= argv
[idx
+ 1]->arg
;
6380 char prefix_str
[BUFSIZ
];
6381 int ret
= netmask_str2prefix_str(prefix
, mask
, prefix_str
);
6384 vty_out(vty
, "%% Inconsistent address and mask\n");
6385 return CMD_WARNING_CONFIG_FAILED
;
6388 return bgp_aggregate_unset(vty
, prefix_str
, AFI_IP
, bgp_node_safi(vty
));
6391 DEFUN (ipv6_aggregate_address
,
6392 ipv6_aggregate_address_cmd
,
6393 "aggregate-address X:X::X:X/M [summary-only]",
6394 "Configure BGP aggregate entries\n"
6395 "Aggregate prefix\n"
6396 "Filter more specific routes from updates\n")
6399 argv_find(argv
, argc
, "X:X::X:X/M", &idx
);
6400 char *prefix
= argv
[idx
]->arg
;
6401 int sum_only
= argv_find(argv
, argc
, "summary-only", &idx
)
6402 ? AGGREGATE_SUMMARY_ONLY
6404 return bgp_aggregate_set(vty
, prefix
, AFI_IP6
, SAFI_UNICAST
, sum_only
,
6408 DEFUN (no_ipv6_aggregate_address
,
6409 no_ipv6_aggregate_address_cmd
,
6410 "no aggregate-address X:X::X:X/M [summary-only]",
6412 "Configure BGP aggregate entries\n"
6413 "Aggregate prefix\n"
6414 "Filter more specific routes from updates\n")
6417 argv_find(argv
, argc
, "X:X::X:X/M", &idx
);
6418 char *prefix
= argv
[idx
]->arg
;
6419 return bgp_aggregate_unset(vty
, prefix
, AFI_IP6
, SAFI_UNICAST
);
6422 /* Redistribute route treatment. */
6423 void bgp_redistribute_add(struct bgp
*bgp
, struct prefix
*p
,
6424 const union g_addr
*nexthop
, ifindex_t ifindex
,
6425 enum nexthop_types_t nhtype
, uint32_t metric
,
6426 uint8_t type
, unsigned short instance
,
6429 struct bgp_path_info
*new;
6430 struct bgp_path_info
*bpi
;
6431 struct bgp_path_info rmap_path
;
6432 struct bgp_node
*bn
;
6434 struct attr
*new_attr
;
6437 struct bgp_redist
*red
;
6439 /* Make default attribute. */
6440 bgp_attr_default_set(&attr
, BGP_ORIGIN_INCOMPLETE
);
6443 case NEXTHOP_TYPE_IFINDEX
:
6445 case NEXTHOP_TYPE_IPV4
:
6446 case NEXTHOP_TYPE_IPV4_IFINDEX
:
6447 attr
.nexthop
= nexthop
->ipv4
;
6449 case NEXTHOP_TYPE_IPV6
:
6450 case NEXTHOP_TYPE_IPV6_IFINDEX
:
6451 attr
.mp_nexthop_global
= nexthop
->ipv6
;
6452 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL
;
6454 case NEXTHOP_TYPE_BLACKHOLE
:
6455 switch (p
->family
) {
6457 attr
.nexthop
.s_addr
= INADDR_ANY
;
6460 memset(&attr
.mp_nexthop_global
, 0,
6461 sizeof(attr
.mp_nexthop_global
));
6462 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL
;
6467 attr
.nh_ifindex
= ifindex
;
6470 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
6473 afi
= family2afi(p
->family
);
6475 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
6477 struct attr attr_new
;
6479 /* Copy attribute for modification. */
6480 bgp_attr_dup(&attr_new
, &attr
);
6482 if (red
->redist_metric_flag
)
6483 attr_new
.med
= red
->redist_metric
;
6485 /* Apply route-map. */
6486 if (red
->rmap
.name
) {
6487 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
6488 rmap_path
.peer
= bgp
->peer_self
;
6489 rmap_path
.attr
= &attr_new
;
6491 SET_FLAG(bgp
->peer_self
->rmap_type
,
6492 PEER_RMAP_TYPE_REDISTRIBUTE
);
6494 ret
= route_map_apply(red
->rmap
.map
, p
, RMAP_BGP
,
6497 bgp
->peer_self
->rmap_type
= 0;
6499 if (ret
== RMAP_DENYMATCH
) {
6500 /* Free uninterned attribute. */
6501 bgp_attr_flush(&attr_new
);
6503 /* Unintern original. */
6504 aspath_unintern(&attr
.aspath
);
6505 bgp_redistribute_delete(bgp
, p
, type
, instance
);
6510 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
))
6511 bgp_attr_add_gshut_community(&attr_new
);
6513 bn
= bgp_afi_node_get(bgp
->rib
[afi
][SAFI_UNICAST
], afi
,
6514 SAFI_UNICAST
, p
, NULL
);
6516 new_attr
= bgp_attr_intern(&attr_new
);
6518 for (bpi
= bgp_node_get_bgp_path_info(bn
); bpi
;
6520 if (bpi
->peer
== bgp
->peer_self
6521 && bpi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
)
6525 /* Ensure the (source route) type is updated. */
6527 if (attrhash_cmp(bpi
->attr
, new_attr
)
6528 && !CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
6529 bgp_attr_unintern(&new_attr
);
6530 aspath_unintern(&attr
.aspath
);
6531 bgp_unlock_node(bn
);
6534 /* The attribute is changed. */
6535 bgp_path_info_set_flag(bn
, bpi
,
6536 BGP_PATH_ATTR_CHANGED
);
6538 /* Rewrite BGP route information. */
6539 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
6540 bgp_path_info_restore(bn
, bpi
);
6542 bgp_aggregate_decrement(
6543 bgp
, p
, bpi
, afi
, SAFI_UNICAST
);
6544 bgp_attr_unintern(&bpi
->attr
);
6545 bpi
->attr
= new_attr
;
6546 bpi
->uptime
= bgp_clock();
6548 /* Process change. */
6549 bgp_aggregate_increment(bgp
, p
, bpi
, afi
,
6551 bgp_process(bgp
, bn
, afi
, SAFI_UNICAST
);
6552 bgp_unlock_node(bn
);
6553 aspath_unintern(&attr
.aspath
);
6555 if ((bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
6557 == BGP_INSTANCE_TYPE_DEFAULT
)) {
6559 vpn_leak_from_vrf_update(
6560 bgp_get_default(), bgp
, bpi
);
6566 new = info_make(type
, BGP_ROUTE_REDISTRIBUTE
, instance
,
6567 bgp
->peer_self
, new_attr
, bn
);
6568 SET_FLAG(new->flags
, BGP_PATH_VALID
);
6570 bgp_aggregate_increment(bgp
, p
, new, afi
, SAFI_UNICAST
);
6571 bgp_path_info_add(bn
, new);
6572 bgp_unlock_node(bn
);
6573 bgp_process(bgp
, bn
, afi
, SAFI_UNICAST
);
6575 if ((bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
6576 || (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
6578 vpn_leak_from_vrf_update(bgp_get_default(), bgp
, new);
6582 /* Unintern original. */
6583 aspath_unintern(&attr
.aspath
);
6586 void bgp_redistribute_delete(struct bgp
*bgp
, struct prefix
*p
, uint8_t type
,
6587 unsigned short instance
)
6590 struct bgp_node
*rn
;
6591 struct bgp_path_info
*pi
;
6592 struct bgp_redist
*red
;
6594 afi
= family2afi(p
->family
);
6596 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
6598 rn
= bgp_afi_node_get(bgp
->rib
[afi
][SAFI_UNICAST
], afi
,
6599 SAFI_UNICAST
, p
, NULL
);
6601 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
6602 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== type
)
6606 if ((bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
6607 || (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
6609 vpn_leak_from_vrf_withdraw(bgp_get_default(),
6612 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, SAFI_UNICAST
);
6613 bgp_path_info_delete(rn
, pi
);
6614 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
6616 bgp_unlock_node(rn
);
6620 /* Withdraw specified route type's route. */
6621 void bgp_redistribute_withdraw(struct bgp
*bgp
, afi_t afi
, int type
,
6622 unsigned short instance
)
6624 struct bgp_node
*rn
;
6625 struct bgp_path_info
*pi
;
6626 struct bgp_table
*table
;
6628 table
= bgp
->rib
[afi
][SAFI_UNICAST
];
6630 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
6631 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
6632 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== type
6633 && pi
->instance
== instance
)
6637 if ((bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
6638 || (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
6640 vpn_leak_from_vrf_withdraw(bgp_get_default(),
6643 bgp_aggregate_decrement(bgp
, &rn
->p
, pi
, afi
,
6645 bgp_path_info_delete(rn
, pi
);
6646 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
6651 /* Static function to display route. */
6652 static void route_vty_out_route(struct prefix
*p
, struct vty
*vty
,
6659 if (p
->family
== AF_INET
) {
6663 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
6666 json_object_string_add(json
, "prefix",
6667 inet_ntop(p
->family
,
6670 json_object_int_add(json
, "prefixLen", p
->prefixlen
);
6671 prefix2str(p
, buf2
, PREFIX_STRLEN
);
6672 json_object_string_add(json
, "network", buf2
);
6674 } else if (p
->family
== AF_ETHERNET
) {
6675 prefix2str(p
, buf
, PREFIX_STRLEN
);
6676 len
= vty_out(vty
, "%s", buf
);
6677 } else if (p
->family
== AF_EVPN
) {
6681 bgp_evpn_route2str((struct prefix_evpn
*)p
, buf
,
6684 bgp_evpn_route2json((struct prefix_evpn
*)p
, json
);
6685 } else if (p
->family
== AF_FLOWSPEC
) {
6686 route_vty_out_flowspec(vty
, p
, NULL
,
6688 NLRI_STRING_FORMAT_JSON_SIMPLE
:
6689 NLRI_STRING_FORMAT_MIN
, json
);
6694 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
6697 json_object_string_add(json
, "prefix",
6698 inet_ntop(p
->family
,
6701 json_object_int_add(json
, "prefixLen", p
->prefixlen
);
6702 prefix2str(p
, buf2
, PREFIX_STRLEN
);
6703 json_object_string_add(json
, "network", buf2
);
6710 vty_out(vty
, "\n%*s", 20, " ");
6712 vty_out(vty
, "%*s", len
, " ");
6716 enum bgp_display_type
{
6720 /* Print the short form route status for a bgp_path_info */
6721 static void route_vty_short_status_out(struct vty
*vty
,
6722 struct bgp_path_info
*path
,
6723 json_object
*json_path
)
6727 /* Route status display. */
6728 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
))
6729 json_object_boolean_true_add(json_path
, "removed");
6731 if (CHECK_FLAG(path
->flags
, BGP_PATH_STALE
))
6732 json_object_boolean_true_add(json_path
, "stale");
6734 if (path
->extra
&& path
->extra
->suppress
)
6735 json_object_boolean_true_add(json_path
, "suppressed");
6737 if (CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)
6738 && !CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
6739 json_object_boolean_true_add(json_path
, "valid");
6742 if (CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
6743 json_object_boolean_true_add(json_path
, "history");
6745 if (CHECK_FLAG(path
->flags
, BGP_PATH_DAMPED
))
6746 json_object_boolean_true_add(json_path
, "damped");
6748 if (CHECK_FLAG(path
->flags
, BGP_PATH_SELECTED
))
6749 json_object_boolean_true_add(json_path
, "bestpath");
6751 if (CHECK_FLAG(path
->flags
, BGP_PATH_MULTIPATH
))
6752 json_object_boolean_true_add(json_path
, "multipath");
6754 /* Internal route. */
6755 if ((path
->peer
->as
)
6756 && (path
->peer
->as
== path
->peer
->local_as
))
6757 json_object_string_add(json_path
, "pathFrom",
6760 json_object_string_add(json_path
, "pathFrom",
6766 /* Route status display. */
6767 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
))
6769 else if (CHECK_FLAG(path
->flags
, BGP_PATH_STALE
))
6771 else if (path
->extra
&& path
->extra
->suppress
)
6773 else if (CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)
6774 && !CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
6780 if (CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
6782 else if (CHECK_FLAG(path
->flags
, BGP_PATH_DAMPED
))
6784 else if (CHECK_FLAG(path
->flags
, BGP_PATH_SELECTED
))
6786 else if (CHECK_FLAG(path
->flags
, BGP_PATH_MULTIPATH
))
6791 /* Internal route. */
6792 if (path
->peer
&& (path
->peer
->as
)
6793 && (path
->peer
->as
== path
->peer
->local_as
))
6799 /* called from terminal list command */
6800 void route_vty_out(struct vty
*vty
, struct prefix
*p
,
6801 struct bgp_path_info
*path
, int display
, safi_t safi
,
6802 json_object
*json_paths
)
6805 json_object
*json_path
= NULL
;
6806 json_object
*json_nexthops
= NULL
;
6807 json_object
*json_nexthop_global
= NULL
;
6808 json_object
*json_nexthop_ll
= NULL
;
6809 char vrf_id_str
[VRF_NAMSIZ
] = {0};
6811 CHECK_FLAG(path
->flags
, BGP_PATH_ANNC_NH_SELF
) ? true : false;
6812 bool nexthop_othervrf
= false;
6813 vrf_id_t nexthop_vrfid
= VRF_DEFAULT
;
6814 const char *nexthop_vrfname
= VRF_DEFAULT_NAME
;
6817 json_path
= json_object_new_object();
6819 /* short status lead text */
6820 route_vty_short_status_out(vty
, path
, json_path
);
6823 /* print prefix and mask */
6825 route_vty_out_route(p
, vty
, json_path
);
6827 vty_out(vty
, "%*s", 17, " ");
6829 route_vty_out_route(p
, vty
, json_path
);
6832 /* Print attribute */
6836 json_object_array_add(json_paths
, json_path
);
6844 * If vrf id of nexthop is different from that of prefix,
6845 * set up printable string to append
6847 if (path
->extra
&& path
->extra
->bgp_orig
) {
6848 const char *self
= "";
6853 nexthop_othervrf
= true;
6854 nexthop_vrfid
= path
->extra
->bgp_orig
->vrf_id
;
6856 if (path
->extra
->bgp_orig
->vrf_id
== VRF_UNKNOWN
)
6857 snprintf(vrf_id_str
, sizeof(vrf_id_str
),
6858 "@%s%s", VRFID_NONE_STR
, self
);
6860 snprintf(vrf_id_str
, sizeof(vrf_id_str
), "@%u%s",
6861 path
->extra
->bgp_orig
->vrf_id
, self
);
6863 if (path
->extra
->bgp_orig
->inst_type
6864 != BGP_INSTANCE_TYPE_DEFAULT
)
6866 nexthop_vrfname
= path
->extra
->bgp_orig
->name
;
6868 const char *self
= "";
6873 snprintf(vrf_id_str
, sizeof(vrf_id_str
), "%s", self
);
6877 * For ENCAP and EVPN routes, nexthop address family is not
6878 * neccessarily the same as the prefix address family.
6879 * Both SAFI_MPLS_VPN and SAFI_ENCAP use the MP nexthop field
6880 * EVPN routes are also exchanged with a MP nexthop. Currently,
6882 * is only IPv4, the value will be present in either
6884 * attr->mp_nexthop_global_in
6886 if ((safi
== SAFI_ENCAP
) || (safi
== SAFI_MPLS_VPN
)) {
6889 int af
= NEXTHOP_FAMILY(attr
->mp_nexthop_len
);
6893 sprintf(nexthop
, "%s",
6894 inet_ntop(af
, &attr
->mp_nexthop_global_in
, buf
,
6898 sprintf(nexthop
, "%s",
6899 inet_ntop(af
, &attr
->mp_nexthop_global
, buf
,
6903 sprintf(nexthop
, "?");
6908 json_nexthop_global
= json_object_new_object();
6910 json_object_string_add(json_nexthop_global
, "afi",
6911 (af
== AF_INET
) ? "ip" : "ipv6");
6912 json_object_string_add(json_nexthop_global
,
6913 (af
== AF_INET
) ? "ip" : "ipv6",
6915 json_object_boolean_true_add(json_nexthop_global
,
6918 vty_out(vty
, "%s%s", nexthop
, vrf_id_str
);
6919 } else if (safi
== SAFI_EVPN
) {
6921 json_nexthop_global
= json_object_new_object();
6923 json_object_string_add(json_nexthop_global
, "ip",
6924 inet_ntoa(attr
->nexthop
));
6925 json_object_string_add(json_nexthop_global
, "afi",
6927 json_object_boolean_true_add(json_nexthop_global
,
6930 vty_out(vty
, "%-16s%s", inet_ntoa(attr
->nexthop
),
6932 } else if (safi
== SAFI_FLOWSPEC
) {
6933 if (attr
->nexthop
.s_addr
!= 0) {
6935 json_nexthop_global
= json_object_new_object();
6936 json_object_string_add(
6937 json_nexthop_global
, "ip",
6938 inet_ntoa(attr
->nexthop
));
6939 json_object_string_add(json_nexthop_global
,
6941 json_object_boolean_true_add(
6942 json_nexthop_global
,
6945 vty_out(vty
, "%-16s", inet_ntoa(attr
->nexthop
));
6948 } else if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(attr
)) {
6950 json_nexthop_global
= json_object_new_object();
6952 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_EVPN
))
6953 json_object_string_add(
6954 json_nexthop_global
, "ip",
6955 inet_ntoa(attr
->mp_nexthop_global_in
));
6957 json_object_string_add(
6958 json_nexthop_global
, "ip",
6959 inet_ntoa(attr
->nexthop
));
6961 json_object_string_add(json_nexthop_global
, "afi",
6963 json_object_boolean_true_add(json_nexthop_global
,
6968 snprintf(buf
, sizeof(buf
), "%s%s",
6969 inet_ntoa(attr
->nexthop
), vrf_id_str
);
6970 vty_out(vty
, "%-16s", buf
);
6975 else if (p
->family
== AF_INET6
|| BGP_ATTR_NEXTHOP_AFI_IP6(attr
)) {
6980 json_nexthop_global
= json_object_new_object();
6981 json_object_string_add(
6982 json_nexthop_global
, "ip",
6983 inet_ntop(AF_INET6
, &attr
->mp_nexthop_global
,
6985 json_object_string_add(json_nexthop_global
, "afi",
6987 json_object_string_add(json_nexthop_global
, "scope",
6990 /* We display both LL & GL if both have been
6992 if ((attr
->mp_nexthop_len
== 32)
6993 || (path
->peer
->conf_if
)) {
6994 json_nexthop_ll
= json_object_new_object();
6995 json_object_string_add(
6996 json_nexthop_ll
, "ip",
6998 &attr
->mp_nexthop_local
, buf
,
7000 json_object_string_add(json_nexthop_ll
, "afi",
7002 json_object_string_add(json_nexthop_ll
, "scope",
7005 if ((IPV6_ADDR_CMP(&attr
->mp_nexthop_global
,
7006 &attr
->mp_nexthop_local
)
7008 && !attr
->mp_nexthop_prefer_global
)
7009 json_object_boolean_true_add(
7010 json_nexthop_ll
, "used");
7012 json_object_boolean_true_add(
7013 json_nexthop_global
, "used");
7015 json_object_boolean_true_add(
7016 json_nexthop_global
, "used");
7018 /* Display LL if LL/Global both in table unless
7019 * prefer-global is set */
7020 if (((attr
->mp_nexthop_len
== 32)
7021 && !attr
->mp_nexthop_prefer_global
)
7022 || (path
->peer
->conf_if
)) {
7023 if (path
->peer
->conf_if
) {
7024 len
= vty_out(vty
, "%s",
7025 path
->peer
->conf_if
);
7026 len
= 16 - len
; /* len of IPv6
7032 vty_out(vty
, "\n%*s", 36, " ");
7034 vty_out(vty
, "%*s", len
, " ");
7040 &attr
->mp_nexthop_local
,
7046 vty_out(vty
, "\n%*s", 36, " ");
7048 vty_out(vty
, "%*s", len
, " ");
7054 &attr
->mp_nexthop_global
, buf
,
7060 vty_out(vty
, "\n%*s", 36, " ");
7062 vty_out(vty
, "%*s", len
, " ");
7068 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
7072 * Adding "metric" field to match with corresponding
7073 * CLI. "med" will be deprecated in future.
7075 json_object_int_add(json_path
, "med", attr
->med
);
7076 json_object_int_add(json_path
, "metric", attr
->med
);
7078 vty_out(vty
, "%10u", attr
->med
);
7079 else if (!json_paths
)
7083 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))
7087 * Adding "locPrf" field to match with corresponding
7088 * CLI. "localPref" will be deprecated in future.
7090 json_object_int_add(json_path
, "localpref",
7092 json_object_int_add(json_path
, "locPrf",
7095 vty_out(vty
, "%7u", attr
->local_pref
);
7096 else if (!json_paths
)
7100 json_object_int_add(json_path
, "weight", attr
->weight
);
7102 vty_out(vty
, "%7u ", attr
->weight
);
7106 json_object_string_add(
7107 json_path
, "peerId",
7108 sockunion2str(&path
->peer
->su
, buf
, SU_ADDRSTRLEN
));
7116 * Adding "path" field to match with corresponding
7117 * CLI. "aspath" will be deprecated in future.
7119 json_object_string_add(json_path
, "aspath",
7121 json_object_string_add(json_path
, "path",
7124 aspath_print_vty(vty
, "%s", attr
->aspath
, " ");
7129 json_object_string_add(json_path
, "origin",
7130 bgp_origin_long_str
[attr
->origin
]);
7132 vty_out(vty
, "%s", bgp_origin_str
[attr
->origin
]);
7136 json_object_boolean_true_add(json_path
,
7137 "announceNexthopSelf");
7138 if (nexthop_othervrf
) {
7139 json_object_string_add(json_path
, "nhVrfName",
7142 json_object_int_add(json_path
, "nhVrfId",
7143 ((nexthop_vrfid
== VRF_UNKNOWN
)
7145 : (int)nexthop_vrfid
));
7150 if (json_nexthop_global
|| json_nexthop_ll
) {
7151 json_nexthops
= json_object_new_array();
7153 if (json_nexthop_global
)
7154 json_object_array_add(json_nexthops
,
7155 json_nexthop_global
);
7157 if (json_nexthop_ll
)
7158 json_object_array_add(json_nexthops
,
7161 json_object_object_add(json_path
, "nexthops",
7165 json_object_array_add(json_paths
, json_path
);
7169 /* prints an additional line, indented, with VNC info, if
7171 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
))
7172 rfapi_vty_out_vncinfo(vty
, p
, path
, safi
);
7177 /* called from terminal list command */
7178 void route_vty_out_tmp(struct vty
*vty
, struct prefix
*p
, struct attr
*attr
,
7179 safi_t safi
, bool use_json
, json_object
*json_ar
)
7181 json_object
*json_status
= NULL
;
7182 json_object
*json_net
= NULL
;
7185 /* Route status display. */
7187 json_status
= json_object_new_object();
7188 json_net
= json_object_new_object();
7195 /* print prefix and mask */
7197 json_object_string_add(
7198 json_net
, "addrPrefix",
7199 inet_ntop(p
->family
, &p
->u
.prefix
, buff
, BUFSIZ
));
7200 json_object_int_add(json_net
, "prefixLen", p
->prefixlen
);
7201 prefix2str(p
, buf2
, PREFIX_STRLEN
);
7202 json_object_string_add(json_net
, "network", buf2
);
7204 route_vty_out_route(p
, vty
, NULL
);
7206 /* Print attribute */
7209 if (p
->family
== AF_INET
7210 && (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7211 || safi
== SAFI_EVPN
7212 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7213 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7214 || safi
== SAFI_EVPN
)
7215 json_object_string_add(
7216 json_net
, "nextHop",
7218 attr
->mp_nexthop_global_in
));
7220 json_object_string_add(
7221 json_net
, "nextHop",
7222 inet_ntoa(attr
->nexthop
));
7223 } else if (p
->family
== AF_INET6
7224 || BGP_ATTR_NEXTHOP_AFI_IP6(attr
)) {
7227 json_object_string_add(
7228 json_net
, "nextHopGlobal",
7230 &attr
->mp_nexthop_global
, buf
,
7235 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
7236 json_object_int_add(json_net
, "metric",
7239 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
7242 * Adding "locPrf" field to match with
7243 * corresponding CLI. "localPref" will be
7244 * deprecated in future.
7246 json_object_int_add(json_net
, "localPref",
7248 json_object_int_add(json_net
, "locPrf",
7252 json_object_int_add(json_net
, "weight", attr
->weight
);
7258 * Adding "path" field to match with
7259 * corresponding CLI. "localPref" will be
7260 * deprecated in future.
7262 json_object_string_add(json_net
, "asPath",
7264 json_object_string_add(json_net
, "path",
7269 json_object_string_add(json_net
, "bgpOriginCode",
7270 bgp_origin_str
[attr
->origin
]);
7272 if (p
->family
== AF_INET
7273 && (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7274 || safi
== SAFI_EVPN
7275 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7276 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7277 || safi
== SAFI_EVPN
)
7278 vty_out(vty
, "%-16s",
7280 attr
->mp_nexthop_global_in
));
7282 vty_out(vty
, "%-16s",
7283 inet_ntoa(attr
->nexthop
));
7284 } else if (p
->family
== AF_INET6
7285 || BGP_ATTR_NEXTHOP_AFI_IP6(attr
)) {
7292 &attr
->mp_nexthop_global
, buf
,
7296 vty_out(vty
, "\n%*s", 36, " ");
7298 vty_out(vty
, "%*s", len
, " ");
7301 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
7302 vty_out(vty
, "%10u", attr
->med
);
7306 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))
7307 vty_out(vty
, "%7u", attr
->local_pref
);
7311 vty_out(vty
, "%7u ", attr
->weight
);
7315 aspath_print_vty(vty
, "%s", attr
->aspath
, " ");
7318 vty_out(vty
, "%s", bgp_origin_str
[attr
->origin
]);
7322 json_object_boolean_true_add(json_status
, "*");
7323 json_object_boolean_true_add(json_status
, ">");
7324 json_object_object_add(json_net
, "appliedStatusSymbols",
7326 char buf_cut
[BUFSIZ
];
7327 json_object_object_add(
7329 inet_ntop(p
->family
, &p
->u
.prefix
, buf_cut
, BUFSIZ
),
7335 void route_vty_out_tag(struct vty
*vty
, struct prefix
*p
,
7336 struct bgp_path_info
*path
, int display
, safi_t safi
,
7339 json_object
*json_out
= NULL
;
7341 mpls_label_t label
= MPLS_INVALID_LABEL
;
7347 json_out
= json_object_new_object();
7349 /* short status lead text */
7350 route_vty_short_status_out(vty
, path
, json_out
);
7352 /* print prefix and mask */
7355 route_vty_out_route(p
, vty
, NULL
);
7357 vty_out(vty
, "%*s", 17, " ");
7360 /* Print attribute */
7363 if (((p
->family
== AF_INET
)
7364 && ((safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
)))
7365 || (safi
== SAFI_EVPN
&& !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
7366 || (!BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7367 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7368 || safi
== SAFI_EVPN
) {
7370 json_object_string_add(
7371 json_out
, "mpNexthopGlobalIn",
7373 attr
->mp_nexthop_global_in
));
7375 vty_out(vty
, "%-16s",
7377 attr
->mp_nexthop_global_in
));
7380 json_object_string_add(
7381 json_out
, "nexthop",
7382 inet_ntoa(attr
->nexthop
));
7384 vty_out(vty
, "%-16s",
7385 inet_ntoa(attr
->nexthop
));
7387 } else if (((p
->family
== AF_INET6
)
7388 && ((safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
)))
7389 || (safi
== SAFI_EVPN
7390 && BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
7391 || (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7395 if (attr
->mp_nexthop_len
7396 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
7398 json_object_string_add(
7399 json_out
, "mpNexthopGlobalIn",
7402 &attr
->mp_nexthop_global
,
7403 buf_a
, sizeof(buf_a
)));
7408 &attr
->mp_nexthop_global
,
7409 buf_a
, sizeof(buf_a
)));
7410 } else if (attr
->mp_nexthop_len
7411 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
7414 &attr
->mp_nexthop_global
,
7415 buf_a
, sizeof(buf_a
));
7417 &attr
->mp_nexthop_local
,
7418 buf_b
, sizeof(buf_b
));
7419 sprintf(buf_c
, "%s(%s)", buf_a
, buf_b
);
7420 json_object_string_add(
7422 "mpNexthopGlobalLocal", buf_c
);
7424 vty_out(vty
, "%s(%s)",
7427 &attr
->mp_nexthop_global
,
7428 buf_a
, sizeof(buf_a
)),
7431 &attr
->mp_nexthop_local
,
7432 buf_b
, sizeof(buf_b
)));
7437 label
= decode_label(&path
->extra
->label
[0]);
7439 if (bgp_is_valid_label(&label
)) {
7441 json_object_int_add(json_out
, "notag", label
);
7442 json_object_array_add(json
, json_out
);
7444 vty_out(vty
, "notag/%d", label
);
7450 void route_vty_out_overlay(struct vty
*vty
, struct prefix
*p
,
7451 struct bgp_path_info
*path
, int display
,
7452 json_object
*json_paths
)
7456 json_object
*json_path
= NULL
;
7459 json_path
= json_object_new_object();
7464 /* short status lead text */
7465 route_vty_short_status_out(vty
, path
, json_path
);
7467 /* print prefix and mask */
7469 route_vty_out_route(p
, vty
, NULL
);
7471 vty_out(vty
, "%*s", 17, " ");
7473 /* Print attribute */
7477 int af
= NEXTHOP_FAMILY(attr
->mp_nexthop_len
);
7481 vty_out(vty
, "%-16s",
7482 inet_ntop(af
, &attr
->mp_nexthop_global_in
, buf
,
7486 vty_out(vty
, "%s(%s)",
7487 inet_ntop(af
, &attr
->mp_nexthop_global
, buf
,
7489 inet_ntop(af
, &attr
->mp_nexthop_local
, buf1
,
7496 char *str
= esi2str(&(attr
->evpn_overlay
.eth_s_id
));
7498 vty_out(vty
, "%s", str
);
7499 XFREE(MTYPE_TMP
, str
);
7501 if (is_evpn_prefix_ipaddr_v4((struct prefix_evpn
*)p
)) {
7503 inet_ntoa(attr
->evpn_overlay
.gw_ip
.ipv4
));
7504 } else if (is_evpn_prefix_ipaddr_v6((struct prefix_evpn
*)p
)) {
7507 &(attr
->evpn_overlay
.gw_ip
.ipv6
), buf
,
7510 if (attr
->ecommunity
) {
7512 struct ecommunity_val
*routermac
= ecommunity_lookup(
7513 attr
->ecommunity
, ECOMMUNITY_ENCODE_EVPN
,
7514 ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC
);
7516 mac
= ecom_mac2str((char *)routermac
->val
);
7518 vty_out(vty
, "/%s", (char *)mac
);
7519 XFREE(MTYPE_TMP
, mac
);
7527 /* dampening route */
7528 static void damp_route_vty_out(struct vty
*vty
, struct prefix
*p
,
7529 struct bgp_path_info
*path
, int display
,
7530 safi_t safi
, bool use_json
, json_object
*json
)
7534 char timebuf
[BGP_UPTIME_LEN
];
7536 /* short status lead text */
7537 route_vty_short_status_out(vty
, path
, json
);
7539 /* print prefix and mask */
7542 route_vty_out_route(p
, vty
, NULL
);
7544 vty_out(vty
, "%*s", 17, " ");
7547 len
= vty_out(vty
, "%s", path
->peer
->host
);
7551 vty_out(vty
, "\n%*s", 34, " ");
7554 json_object_int_add(json
, "peerHost", len
);
7556 vty_out(vty
, "%*s", len
, " ");
7560 bgp_damp_reuse_time_vty(vty
, path
, timebuf
, BGP_UPTIME_LEN
,
7564 bgp_damp_reuse_time_vty(vty
, path
, timebuf
,
7565 BGP_UPTIME_LEN
, use_json
,
7568 /* Print attribute */
7574 json_object_string_add(json
, "asPath",
7577 aspath_print_vty(vty
, "%s", attr
->aspath
, " ");
7582 json_object_string_add(json
, "origin",
7583 bgp_origin_str
[attr
->origin
]);
7585 vty_out(vty
, "%s", bgp_origin_str
[attr
->origin
]);
7592 static void flap_route_vty_out(struct vty
*vty
, struct prefix
*p
,
7593 struct bgp_path_info
*path
, int display
,
7594 safi_t safi
, bool use_json
, json_object
*json
)
7597 struct bgp_damp_info
*bdi
;
7598 char timebuf
[BGP_UPTIME_LEN
];
7604 bdi
= path
->extra
->damp_info
;
7606 /* short status lead text */
7607 route_vty_short_status_out(vty
, path
, json
);
7609 /* print prefix and mask */
7612 route_vty_out_route(p
, vty
, NULL
);
7614 vty_out(vty
, "%*s", 17, " ");
7617 len
= vty_out(vty
, "%s", path
->peer
->host
);
7621 vty_out(vty
, "\n%*s", 33, " ");
7624 json_object_int_add(json
, "peerHost", len
);
7626 vty_out(vty
, "%*s", len
, " ");
7629 len
= vty_out(vty
, "%d", bdi
->flap
);
7636 json_object_int_add(json
, "bdiFlap", len
);
7638 vty_out(vty
, "%*s", len
, " ");
7642 peer_uptime(bdi
->start_time
, timebuf
, BGP_UPTIME_LEN
, use_json
,
7645 vty_out(vty
, "%s ", peer_uptime(bdi
->start_time
, timebuf
,
7646 BGP_UPTIME_LEN
, 0, NULL
));
7648 if (CHECK_FLAG(path
->flags
, BGP_PATH_DAMPED
)
7649 && !CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
)) {
7651 bgp_damp_reuse_time_vty(vty
, path
, timebuf
,
7652 BGP_UPTIME_LEN
, use_json
, json
);
7655 bgp_damp_reuse_time_vty(vty
, path
, timebuf
,
7660 vty_out(vty
, "%*s ", 8, " ");
7663 /* Print attribute */
7669 json_object_string_add(json
, "asPath",
7672 aspath_print_vty(vty
, "%s", attr
->aspath
, " ");
7677 json_object_string_add(json
, "origin",
7678 bgp_origin_str
[attr
->origin
]);
7680 vty_out(vty
, "%s", bgp_origin_str
[attr
->origin
]);
7686 static void route_vty_out_advertised_to(struct vty
*vty
, struct peer
*peer
,
7687 int *first
, const char *header
,
7688 json_object
*json_adv_to
)
7690 char buf1
[INET6_ADDRSTRLEN
];
7691 json_object
*json_peer
= NULL
;
7694 /* 'advertised-to' is a dictionary of peers we have advertised
7696 * prefix too. The key is the peer's IP or swpX, the value is
7698 * hostname if we know it and "" if not.
7700 json_peer
= json_object_new_object();
7703 json_object_string_add(json_peer
, "hostname",
7707 json_object_object_add(json_adv_to
, peer
->conf_if
,
7710 json_object_object_add(
7712 sockunion2str(&peer
->su
, buf1
, SU_ADDRSTRLEN
),
7716 vty_out(vty
, "%s", header
);
7721 && bgp_flag_check(peer
->bgp
, BGP_FLAG_SHOW_HOSTNAME
)) {
7723 vty_out(vty
, " %s(%s)", peer
->hostname
,
7726 vty_out(vty
, " %s(%s)", peer
->hostname
,
7727 sockunion2str(&peer
->su
, buf1
,
7731 vty_out(vty
, " %s", peer
->conf_if
);
7734 sockunion2str(&peer
->su
, buf1
,
7740 static void route_vty_out_tx_ids(struct vty
*vty
,
7741 struct bgp_addpath_info_data
*d
)
7745 for (i
= 0; i
< BGP_ADDPATH_MAX
; i
++) {
7746 vty_out(vty
, "TX-%s %u%s", bgp_addpath_names(i
)->human_name
,
7747 d
->addpath_tx_id
[i
],
7748 i
< BGP_ADDPATH_MAX
- 1 ? " " : "\n");
7752 void route_vty_out_detail(struct vty
*vty
, struct bgp
*bgp
, struct prefix
*p
,
7753 struct bgp_path_info
*path
, afi_t afi
, safi_t safi
,
7754 json_object
*json_paths
)
7756 char buf
[INET6_ADDRSTRLEN
];
7758 char buf2
[EVPN_ROUTE_STRLEN
];
7760 int sockunion_vty_out(struct vty
*, union sockunion
*);
7762 json_object
*json_bestpath
= NULL
;
7763 json_object
*json_cluster_list
= NULL
;
7764 json_object
*json_cluster_list_list
= NULL
;
7765 json_object
*json_ext_community
= NULL
;
7766 json_object
*json_last_update
= NULL
;
7767 json_object
*json_pmsi
= NULL
;
7768 json_object
*json_nexthop_global
= NULL
;
7769 json_object
*json_nexthop_ll
= NULL
;
7770 json_object
*json_nexthops
= NULL
;
7771 json_object
*json_path
= NULL
;
7772 json_object
*json_peer
= NULL
;
7773 json_object
*json_string
= NULL
;
7774 json_object
*json_adv_to
= NULL
;
7776 struct listnode
*node
, *nnode
;
7778 int addpath_capable
;
7780 unsigned int first_as
;
7782 CHECK_FLAG(path
->flags
, BGP_PATH_ANNC_NH_SELF
) ? true : false;
7786 json_path
= json_object_new_object();
7787 json_peer
= json_object_new_object();
7788 json_nexthop_global
= json_object_new_object();
7791 if (!json_paths
&& safi
== SAFI_EVPN
) {
7794 bgp_evpn_route2str((struct prefix_evpn
*)p
, buf2
, sizeof(buf2
));
7795 vty_out(vty
, " Route %s", buf2
);
7797 if (path
->extra
&& path
->extra
->num_labels
) {
7798 bgp_evpn_label2str(path
->extra
->label
,
7799 path
->extra
->num_labels
, tag_buf
,
7801 vty_out(vty
, " VNI %s", tag_buf
);
7804 if (path
->extra
&& path
->extra
->parent
) {
7805 struct bgp_path_info
*parent_ri
;
7806 struct bgp_node
*rn
, *prn
;
7808 parent_ri
= (struct bgp_path_info
*)path
->extra
->parent
;
7809 rn
= parent_ri
->net
;
7810 if (rn
&& rn
->prn
) {
7812 vty_out(vty
, " Imported from %s:%s\n",
7814 (struct prefix_rd
*)&prn
->p
,
7815 buf1
, sizeof(buf1
)),
7824 /* Line1 display AS-path, Aggregator */
7827 if (!attr
->aspath
->json
)
7828 aspath_str_update(attr
->aspath
, true);
7829 json_object_lock(attr
->aspath
->json
);
7830 json_object_object_add(json_path
, "aspath",
7831 attr
->aspath
->json
);
7833 if (attr
->aspath
->segments
)
7834 aspath_print_vty(vty
, " %s",
7837 vty_out(vty
, " Local");
7841 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
)) {
7843 json_object_boolean_true_add(json_path
,
7846 vty_out(vty
, ", (removed)");
7849 if (CHECK_FLAG(path
->flags
, BGP_PATH_STALE
)) {
7851 json_object_boolean_true_add(json_path
,
7854 vty_out(vty
, ", (stale)");
7857 if (CHECK_FLAG(attr
->flag
,
7858 ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
))) {
7860 json_object_int_add(json_path
, "aggregatorAs",
7861 attr
->aggregator_as
);
7862 json_object_string_add(
7863 json_path
, "aggregatorId",
7864 inet_ntoa(attr
->aggregator_addr
));
7866 vty_out(vty
, ", (aggregated by %u %s)",
7867 attr
->aggregator_as
,
7868 inet_ntoa(attr
->aggregator_addr
));
7872 if (CHECK_FLAG(path
->peer
->af_flags
[afi
][safi
],
7873 PEER_FLAG_REFLECTOR_CLIENT
)) {
7875 json_object_boolean_true_add(
7876 json_path
, "rxedFromRrClient");
7878 vty_out(vty
, ", (Received from a RR-client)");
7881 if (CHECK_FLAG(path
->peer
->af_flags
[afi
][safi
],
7882 PEER_FLAG_RSERVER_CLIENT
)) {
7884 json_object_boolean_true_add(
7885 json_path
, "rxedFromRsClient");
7887 vty_out(vty
, ", (Received from a RS-client)");
7890 if (CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
)) {
7892 json_object_boolean_true_add(
7893 json_path
, "dampeningHistoryEntry");
7895 vty_out(vty
, ", (history entry)");
7896 } else if (CHECK_FLAG(path
->flags
, BGP_PATH_DAMPED
)) {
7898 json_object_boolean_true_add(
7899 json_path
, "dampeningSuppressed");
7901 vty_out(vty
, ", (suppressed due to dampening)");
7907 /* Line2 display Next-hop, Neighbor, Router-id */
7908 /* Display the nexthop */
7909 if ((p
->family
== AF_INET
|| p
->family
== AF_ETHERNET
7910 || p
->family
== AF_EVPN
)
7911 && (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7912 || safi
== SAFI_EVPN
7913 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7914 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7915 || safi
== SAFI_EVPN
) {
7917 json_object_string_add(
7918 json_nexthop_global
, "ip",
7920 attr
->mp_nexthop_global_in
));
7924 attr
->mp_nexthop_global_in
));
7927 json_object_string_add(
7928 json_nexthop_global
, "ip",
7929 inet_ntoa(attr
->nexthop
));
7932 inet_ntoa(attr
->nexthop
));
7936 json_object_string_add(json_nexthop_global
,
7940 json_object_string_add(
7941 json_nexthop_global
, "ip",
7943 &attr
->mp_nexthop_global
, buf
,
7945 json_object_string_add(json_nexthop_global
,
7947 json_object_string_add(json_nexthop_global
,
7952 &attr
->mp_nexthop_global
, buf
,
7957 /* Display the IGP cost or 'inaccessible' */
7958 if (!CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)) {
7960 json_object_boolean_false_add(
7961 json_nexthop_global
, "accessible");
7963 vty_out(vty
, " (inaccessible)");
7965 if (path
->extra
&& path
->extra
->igpmetric
) {
7967 json_object_int_add(
7968 json_nexthop_global
, "metric",
7969 path
->extra
->igpmetric
);
7971 vty_out(vty
, " (metric %u)",
7972 path
->extra
->igpmetric
);
7975 /* IGP cost is 0, display this only for json */
7978 json_object_int_add(json_nexthop_global
,
7983 json_object_boolean_true_add(
7984 json_nexthop_global
, "accessible");
7987 /* Display peer "from" output */
7988 /* This path was originated locally */
7989 if (path
->peer
== bgp
->peer_self
) {
7991 if (safi
== SAFI_EVPN
7992 || (p
->family
== AF_INET
7993 && !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7995 json_object_string_add(
7996 json_peer
, "peerId", "0.0.0.0");
7998 vty_out(vty
, " from 0.0.0.0 ");
8001 json_object_string_add(json_peer
,
8004 vty_out(vty
, " from :: ");
8008 json_object_string_add(
8009 json_peer
, "routerId",
8010 inet_ntoa(bgp
->router_id
));
8012 vty_out(vty
, "(%s)", inet_ntoa(bgp
->router_id
));
8015 /* We RXed this path from one of our peers */
8019 json_object_string_add(
8020 json_peer
, "peerId",
8021 sockunion2str(&path
->peer
->su
, buf
,
8023 json_object_string_add(
8024 json_peer
, "routerId",
8026 &path
->peer
->remote_id
, buf1
,
8029 if (path
->peer
->hostname
)
8030 json_object_string_add(
8031 json_peer
, "hostname",
8032 path
->peer
->hostname
);
8034 if (path
->peer
->domainname
)
8035 json_object_string_add(
8036 json_peer
, "domainname",
8037 path
->peer
->domainname
);
8039 if (path
->peer
->conf_if
)
8040 json_object_string_add(
8041 json_peer
, "interface",
8042 path
->peer
->conf_if
);
8044 if (path
->peer
->conf_if
) {
8045 if (path
->peer
->hostname
8048 BGP_FLAG_SHOW_HOSTNAME
))
8049 vty_out(vty
, " from %s(%s)",
8050 path
->peer
->hostname
,
8051 path
->peer
->conf_if
);
8053 vty_out(vty
, " from %s",
8054 path
->peer
->conf_if
);
8056 if (path
->peer
->hostname
8059 BGP_FLAG_SHOW_HOSTNAME
))
8060 vty_out(vty
, " from %s(%s)",
8061 path
->peer
->hostname
,
8064 vty_out(vty
, " from %s",
8072 & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
8073 vty_out(vty
, " (%s)",
8074 inet_ntoa(attr
->originator_id
));
8076 vty_out(vty
, " (%s)",
8079 &path
->peer
->remote_id
,
8080 buf1
, sizeof(buf1
)));
8085 * Note when vrfid of nexthop is different from that of prefix
8087 if (path
->extra
&& path
->extra
->bgp_orig
) {
8088 vrf_id_t nexthop_vrfid
= path
->extra
->bgp_orig
->vrf_id
;
8093 if (path
->extra
->bgp_orig
->inst_type
8094 == BGP_INSTANCE_TYPE_DEFAULT
)
8096 vn
= VRF_DEFAULT_NAME
;
8098 vn
= path
->extra
->bgp_orig
->name
;
8100 json_object_string_add(json_path
, "nhVrfName",
8103 if (nexthop_vrfid
== VRF_UNKNOWN
) {
8104 json_object_int_add(json_path
,
8107 json_object_int_add(json_path
,
8108 "nhVrfId", (int)nexthop_vrfid
);
8111 if (nexthop_vrfid
== VRF_UNKNOWN
)
8112 vty_out(vty
, " vrf ?");
8114 vty_out(vty
, " vrf %u", nexthop_vrfid
);
8120 json_object_boolean_true_add(json_path
,
8121 "announceNexthopSelf");
8123 vty_out(vty
, " announce-nh-self");
8130 /* display the link-local nexthop */
8131 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
8133 json_nexthop_ll
= json_object_new_object();
8134 json_object_string_add(
8135 json_nexthop_ll
, "ip",
8137 &attr
->mp_nexthop_local
, buf
,
8139 json_object_string_add(json_nexthop_ll
, "afi",
8141 json_object_string_add(json_nexthop_ll
, "scope",
8144 json_object_boolean_true_add(json_nexthop_ll
,
8147 if (!attr
->mp_nexthop_prefer_global
)
8148 json_object_boolean_true_add(
8149 json_nexthop_ll
, "used");
8151 json_object_boolean_true_add(
8152 json_nexthop_global
, "used");
8154 vty_out(vty
, " (%s) %s\n",
8156 &attr
->mp_nexthop_local
, buf
,
8158 attr
->mp_nexthop_prefer_global
8163 /* If we do not have a link-local nexthop then we must flag the
8167 json_object_boolean_true_add(
8168 json_nexthop_global
, "used");
8171 /* Line 3 display Origin, Med, Locpref, Weight, Tag, valid,
8172 * Int/Ext/Local, Atomic, best */
8174 json_object_string_add(
8175 json_path
, "origin",
8176 bgp_origin_long_str
[attr
->origin
]);
8178 vty_out(vty
, " Origin %s",
8179 bgp_origin_long_str
[attr
->origin
]);
8181 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
8185 * Adding "metric" field to match with
8186 * corresponding CLI. "med" will be
8187 * deprecated in future.
8189 json_object_int_add(json_path
, "med",
8191 json_object_int_add(json_path
, "metric",
8194 vty_out(vty
, ", metric %u", attr
->med
);
8197 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
8199 json_object_int_add(json_path
, "localpref",
8202 vty_out(vty
, ", localpref %u",
8206 if (attr
->weight
!= 0) {
8208 json_object_int_add(json_path
, "weight",
8211 vty_out(vty
, ", weight %u", attr
->weight
);
8214 if (attr
->tag
!= 0) {
8216 json_object_int_add(json_path
, "tag",
8219 vty_out(vty
, ", tag %" ROUTE_TAG_PRI
,
8223 if (!CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)) {
8225 json_object_boolean_false_add(json_path
,
8228 vty_out(vty
, ", invalid");
8229 } else if (!CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
)) {
8231 json_object_boolean_true_add(json_path
,
8234 vty_out(vty
, ", valid");
8237 if (path
->peer
!= bgp
->peer_self
) {
8238 if (path
->peer
->as
== path
->peer
->local_as
) {
8239 if (CHECK_FLAG(bgp
->config
,
8240 BGP_CONFIG_CONFEDERATION
)) {
8242 json_object_string_add(
8247 ", confed-internal");
8250 json_object_string_add(
8254 vty_out(vty
, ", internal");
8257 if (bgp_confederation_peers_check(
8258 bgp
, path
->peer
->as
)) {
8260 json_object_string_add(
8265 ", confed-external");
8268 json_object_string_add(
8272 vty_out(vty
, ", external");
8275 } else if (path
->sub_type
== BGP_ROUTE_AGGREGATE
) {
8277 json_object_boolean_true_add(json_path
,
8279 json_object_boolean_true_add(json_path
,
8282 vty_out(vty
, ", aggregated, local");
8284 } else if (path
->type
!= ZEBRA_ROUTE_BGP
) {
8286 json_object_boolean_true_add(json_path
,
8289 vty_out(vty
, ", sourced");
8292 json_object_boolean_true_add(json_path
,
8294 json_object_boolean_true_add(json_path
,
8297 vty_out(vty
, ", sourced, local");
8301 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
8303 json_object_boolean_true_add(json_path
,
8306 vty_out(vty
, ", atomic-aggregate");
8309 if (CHECK_FLAG(path
->flags
, BGP_PATH_MULTIPATH
)
8310 || (CHECK_FLAG(path
->flags
, BGP_PATH_SELECTED
)
8311 && bgp_path_info_mpath_count(path
))) {
8313 json_object_boolean_true_add(json_path
,
8316 vty_out(vty
, ", multipath");
8319 // Mark the bestpath(s)
8320 if (CHECK_FLAG(path
->flags
, BGP_PATH_DMED_SELECTED
)) {
8321 first_as
= aspath_get_first_as(attr
->aspath
);
8326 json_object_new_object();
8327 json_object_int_add(json_bestpath
,
8328 "bestpathFromAs", first_as
);
8331 vty_out(vty
, ", bestpath-from-AS %u",
8335 ", bestpath-from-AS Local");
8339 if (CHECK_FLAG(path
->flags
, BGP_PATH_SELECTED
)) {
8343 json_object_new_object();
8344 json_object_boolean_true_add(json_bestpath
,
8347 vty_out(vty
, ", best");
8351 json_object_object_add(json_path
, "bestpath",
8357 /* Line 4 display Community */
8358 if (attr
->community
) {
8360 if (!attr
->community
->json
)
8361 community_str(attr
->community
, true);
8362 json_object_lock(attr
->community
->json
);
8363 json_object_object_add(json_path
, "community",
8364 attr
->community
->json
);
8366 vty_out(vty
, " Community: %s\n",
8367 attr
->community
->str
);
8371 /* Line 5 display Extended-community */
8372 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)) {
8374 json_ext_community
= json_object_new_object();
8375 json_object_string_add(json_ext_community
,
8377 attr
->ecommunity
->str
);
8378 json_object_object_add(json_path
,
8379 "extendedCommunity",
8380 json_ext_community
);
8382 vty_out(vty
, " Extended Community: %s\n",
8383 attr
->ecommunity
->str
);
8387 /* Line 6 display Large community */
8388 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
)) {
8390 if (!attr
->lcommunity
->json
)
8391 lcommunity_str(attr
->lcommunity
, true);
8392 json_object_lock(attr
->lcommunity
->json
);
8393 json_object_object_add(json_path
,
8395 attr
->lcommunity
->json
);
8397 vty_out(vty
, " Large Community: %s\n",
8398 attr
->lcommunity
->str
);
8402 /* Line 7 display Originator, Cluster-id */
8403 if ((attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
8404 || (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
))) {
8406 & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
)) {
8408 json_object_string_add(
8409 json_path
, "originatorId",
8410 inet_ntoa(attr
->originator_id
));
8412 vty_out(vty
, " Originator: %s",
8413 inet_ntoa(attr
->originator_id
));
8416 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
)) {
8421 json_object_new_object();
8422 json_cluster_list_list
=
8423 json_object_new_array();
8426 i
< attr
->cluster
->length
/ 4;
8428 json_string
= json_object_new_string(
8432 json_object_array_add(
8433 json_cluster_list_list
,
8437 /* struct cluster_list does not have
8439 * aspath and community do. Add this
8442 json_object_string_add(json_cluster_list,
8443 "string", attr->cluster->str);
8445 json_object_object_add(
8446 json_cluster_list
, "list",
8447 json_cluster_list_list
);
8448 json_object_object_add(
8449 json_path
, "clusterList",
8452 vty_out(vty
, ", Cluster list: ");
8455 i
< attr
->cluster
->length
/ 4;
8469 if (path
->extra
&& path
->extra
->damp_info
)
8470 bgp_damp_info_vty(vty
, path
, json_path
);
8473 if (path
->extra
&& bgp_is_valid_label(&path
->extra
->label
[0])
8474 && safi
!= SAFI_EVPN
) {
8475 mpls_label_t label
= label_pton(&path
->extra
->label
[0]);
8478 json_object_int_add(json_path
, "remoteLabel",
8481 vty_out(vty
, " Remote label: %d\n", label
);
8485 if (attr
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
8487 json_object_int_add(json_path
, "labelIndex",
8490 vty_out(vty
, " Label Index: %d\n",
8494 /* Line 8 display Addpath IDs */
8495 if (path
->addpath_rx_id
8496 || bgp_addpath_info_has_ids(&path
->tx_addpath
)) {
8498 json_object_int_add(json_path
, "addpathRxId",
8499 path
->addpath_rx_id
);
8501 /* Keep backwards compatibility with the old API
8502 * by putting TX All's ID in the old field
8504 json_object_int_add(
8505 json_path
, "addpathTxId",
8506 path
->tx_addpath
.addpath_tx_id
8509 /* ... but create a specific field for each
8512 for (i
= 0; i
< BGP_ADDPATH_MAX
; i
++) {
8513 json_object_int_add(
8515 bgp_addpath_names(i
)
8521 vty_out(vty
, " AddPath ID: RX %u, ",
8522 path
->addpath_rx_id
);
8524 route_vty_out_tx_ids(vty
, &path
->tx_addpath
);
8528 /* If we used addpath to TX a non-bestpath we need to display
8529 * "Advertised to" on a path-by-path basis
8531 if (bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
8534 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8536 bgp_addpath_encode_tx(peer
, afi
, safi
);
8537 has_adj
= bgp_adj_out_lookup(
8539 bgp_addpath_id_for_peer(
8541 &path
->tx_addpath
));
8543 if ((addpath_capable
&& has_adj
)
8544 || (!addpath_capable
&& has_adj
8545 && CHECK_FLAG(path
->flags
,
8546 BGP_PATH_SELECTED
))) {
8547 if (json_path
&& !json_adv_to
)
8549 json_object_new_object();
8551 route_vty_out_advertised_to(
8560 json_object_object_add(json_path
,
8571 /* Line 9 display Uptime */
8572 tbuf
= time(NULL
) - (bgp_clock() - path
->uptime
);
8574 json_last_update
= json_object_new_object();
8575 json_object_int_add(json_last_update
, "epoch", tbuf
);
8576 json_object_string_add(json_last_update
, "string",
8578 json_object_object_add(json_path
, "lastUpdate",
8581 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
8583 /* Line 10 display PMSI tunnel attribute, if present */
8584 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
)) {
8585 const char *str
= lookup_msg(bgp_pmsi_tnltype_str
,
8586 attr
->pmsi_tnl_type
,
8587 PMSI_TNLTYPE_STR_DEFAULT
);
8590 json_pmsi
= json_object_new_object();
8591 json_object_string_add(json_pmsi
,
8593 json_object_object_add(json_path
, "pmsi",
8596 vty_out(vty
, " PMSI Tunnel Type: %s\n",
8602 /* We've constructed the json object for this path, add it to the json
8606 if (json_nexthop_global
|| json_nexthop_ll
) {
8607 json_nexthops
= json_object_new_array();
8609 if (json_nexthop_global
)
8610 json_object_array_add(json_nexthops
,
8611 json_nexthop_global
);
8613 if (json_nexthop_ll
)
8614 json_object_array_add(json_nexthops
,
8617 json_object_object_add(json_path
, "nexthops",
8621 json_object_object_add(json_path
, "peer", json_peer
);
8622 json_object_array_add(json_paths
, json_path
);
8627 #define BGP_SHOW_HEADER_CSV "Flags, Network, Next Hop, Metric, LocPrf, Weight, Path"
8628 #define BGP_SHOW_DAMP_HEADER " Network From Reuse Path\n"
8629 #define BGP_SHOW_FLAP_HEADER " Network From Flaps Duration Reuse Path\n"
8631 static int bgp_show_prefix_list(struct vty
*vty
, struct bgp
*bgp
,
8632 const char *prefix_list_str
, afi_t afi
,
8633 safi_t safi
, enum bgp_show_type type
);
8634 static int bgp_show_filter_list(struct vty
*vty
, struct bgp
*bgp
,
8635 const char *filter
, afi_t afi
, safi_t safi
,
8636 enum bgp_show_type type
);
8637 static int bgp_show_route_map(struct vty
*vty
, struct bgp
*bgp
,
8638 const char *rmap_str
, afi_t afi
, safi_t safi
,
8639 enum bgp_show_type type
);
8640 static int bgp_show_community_list(struct vty
*vty
, struct bgp
*bgp
,
8641 const char *com
, int exact
, afi_t afi
,
8643 static int bgp_show_prefix_longer(struct vty
*vty
, struct bgp
*bgp
,
8644 const char *prefix
, afi_t afi
, safi_t safi
,
8645 enum bgp_show_type type
);
8646 static int bgp_show_regexp(struct vty
*vty
, struct bgp
*bgp
, const char *regstr
,
8647 afi_t afi
, safi_t safi
, enum bgp_show_type type
);
8648 static int bgp_show_community(struct vty
*vty
, struct bgp
*bgp
,
8649 const char *comstr
, int exact
, afi_t afi
,
8650 safi_t safi
, bool use_json
);
8653 static int bgp_show_table(struct vty
*vty
, struct bgp
*bgp
, safi_t safi
,
8654 struct bgp_table
*table
, enum bgp_show_type type
,
8655 void *output_arg
, bool use_json
, char *rd
,
8656 int is_last
, unsigned long *output_cum
,
8657 unsigned long *total_cum
,
8658 unsigned long *json_header_depth
)
8660 struct bgp_path_info
*pi
;
8661 struct bgp_node
*rn
;
8664 unsigned long output_count
= 0;
8665 unsigned long total_count
= 0;
8668 json_object
*json_paths
= NULL
;
8671 if (output_cum
&& *output_cum
!= 0)
8674 if (use_json
&& !*json_header_depth
) {
8676 "{\n \"vrfId\": %d,\n \"vrfName\": \"%s\",\n \"tableVersion\": %" PRId64
8677 ",\n \"routerId\": \"%s\",\n \"defaultLocPrf\": %u,\n"
8678 " \"localAS\": %u,\n \"routes\": { ",
8679 bgp
->vrf_id
== VRF_UNKNOWN
? -1 : (int)bgp
->vrf_id
,
8680 bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
8683 table
->version
, inet_ntoa(bgp
->router_id
),
8684 bgp
->default_local_pref
, bgp
->as
);
8685 *json_header_depth
= 2;
8687 vty_out(vty
, " \"routeDistinguishers\" : {");
8688 ++*json_header_depth
;
8692 if (use_json
&& rd
) {
8693 vty_out(vty
, " \"%s\" : { ", rd
);
8696 /* Start processing of routes. */
8697 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
8698 pi
= bgp_node_get_bgp_path_info(rn
);
8704 json_paths
= json_object_new_array();
8708 for (; pi
; pi
= pi
->next
) {
8710 if (type
== bgp_show_type_flap_statistics
8711 || type
== bgp_show_type_flap_neighbor
8712 || type
== bgp_show_type_dampend_paths
8713 || type
== bgp_show_type_damp_neighbor
) {
8714 if (!(pi
->extra
&& pi
->extra
->damp_info
))
8717 if (type
== bgp_show_type_regexp
) {
8718 regex_t
*regex
= output_arg
;
8720 if (bgp_regexec(regex
, pi
->attr
->aspath
)
8724 if (type
== bgp_show_type_prefix_list
) {
8725 struct prefix_list
*plist
= output_arg
;
8727 if (prefix_list_apply(plist
, &rn
->p
)
8731 if (type
== bgp_show_type_filter_list
) {
8732 struct as_list
*as_list
= output_arg
;
8734 if (as_list_apply(as_list
, pi
->attr
->aspath
)
8735 != AS_FILTER_PERMIT
)
8738 if (type
== bgp_show_type_route_map
) {
8739 struct route_map
*rmap
= output_arg
;
8740 struct bgp_path_info path
;
8741 struct attr dummy_attr
;
8744 bgp_attr_dup(&dummy_attr
, pi
->attr
);
8746 path
.peer
= pi
->peer
;
8747 path
.attr
= &dummy_attr
;
8749 ret
= route_map_apply(rmap
, &rn
->p
, RMAP_BGP
,
8751 if (ret
== RMAP_DENYMATCH
)
8754 if (type
== bgp_show_type_neighbor
8755 || type
== bgp_show_type_flap_neighbor
8756 || type
== bgp_show_type_damp_neighbor
) {
8757 union sockunion
*su
= output_arg
;
8759 if (pi
->peer
== NULL
8760 || pi
->peer
->su_remote
== NULL
8761 || !sockunion_same(pi
->peer
->su_remote
, su
))
8764 if (type
== bgp_show_type_cidr_only
) {
8765 uint32_t destination
;
8767 destination
= ntohl(rn
->p
.u
.prefix4
.s_addr
);
8768 if (IN_CLASSC(destination
)
8769 && rn
->p
.prefixlen
== 24)
8771 if (IN_CLASSB(destination
)
8772 && rn
->p
.prefixlen
== 16)
8774 if (IN_CLASSA(destination
)
8775 && rn
->p
.prefixlen
== 8)
8778 if (type
== bgp_show_type_prefix_longer
) {
8780 if (!prefix_match(p
, &rn
->p
))
8783 if (type
== bgp_show_type_community_all
) {
8784 if (!pi
->attr
->community
)
8787 if (type
== bgp_show_type_community
) {
8788 struct community
*com
= output_arg
;
8790 if (!pi
->attr
->community
8791 || !community_match(pi
->attr
->community
,
8795 if (type
== bgp_show_type_community_exact
) {
8796 struct community
*com
= output_arg
;
8798 if (!pi
->attr
->community
8799 || !community_cmp(pi
->attr
->community
, com
))
8802 if (type
== bgp_show_type_community_list
) {
8803 struct community_list
*list
= output_arg
;
8805 if (!community_list_match(pi
->attr
->community
,
8809 if (type
== bgp_show_type_community_list_exact
) {
8810 struct community_list
*list
= output_arg
;
8812 if (!community_list_exact_match(
8813 pi
->attr
->community
, list
))
8816 if (type
== bgp_show_type_lcommunity
) {
8817 struct lcommunity
*lcom
= output_arg
;
8819 if (!pi
->attr
->lcommunity
8820 || !lcommunity_match(pi
->attr
->lcommunity
,
8824 if (type
== bgp_show_type_lcommunity_list
) {
8825 struct community_list
*list
= output_arg
;
8827 if (!lcommunity_list_match(pi
->attr
->lcommunity
,
8831 if (type
== bgp_show_type_lcommunity_all
) {
8832 if (!pi
->attr
->lcommunity
)
8835 if (type
== bgp_show_type_dampend_paths
8836 || type
== bgp_show_type_damp_neighbor
) {
8837 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_DAMPED
)
8838 || CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
))
8842 if (!use_json
&& header
) {
8843 vty_out(vty
, "BGP table version is %" PRIu64
8844 ", local router ID is %s, vrf id ",
8846 inet_ntoa(bgp
->router_id
));
8847 if (bgp
->vrf_id
== VRF_UNKNOWN
)
8848 vty_out(vty
, "%s", VRFID_NONE_STR
);
8850 vty_out(vty
, "%u", bgp
->vrf_id
);
8852 vty_out(vty
, "Default local pref %u, ",
8853 bgp
->default_local_pref
);
8854 vty_out(vty
, "local AS %u\n", bgp
->as
);
8855 vty_out(vty
, BGP_SHOW_SCODE_HEADER
);
8856 vty_out(vty
, BGP_SHOW_NCODE_HEADER
);
8857 vty_out(vty
, BGP_SHOW_OCODE_HEADER
);
8858 if (type
== bgp_show_type_dampend_paths
8859 || type
== bgp_show_type_damp_neighbor
)
8860 vty_out(vty
, BGP_SHOW_DAMP_HEADER
);
8861 else if (type
== bgp_show_type_flap_statistics
8862 || type
== bgp_show_type_flap_neighbor
)
8863 vty_out(vty
, BGP_SHOW_FLAP_HEADER
);
8865 vty_out(vty
, BGP_SHOW_HEADER
);
8868 if (rd
!= NULL
&& !display
&& !output_count
) {
8871 "Route Distinguisher: %s\n",
8874 if (type
== bgp_show_type_dampend_paths
8875 || type
== bgp_show_type_damp_neighbor
)
8876 damp_route_vty_out(vty
, &rn
->p
, pi
, display
,
8877 safi
, use_json
, json_paths
);
8878 else if (type
== bgp_show_type_flap_statistics
8879 || type
== bgp_show_type_flap_neighbor
)
8880 flap_route_vty_out(vty
, &rn
->p
, pi
, display
,
8881 safi
, use_json
, json_paths
);
8883 route_vty_out(vty
, &rn
->p
, pi
, display
, safi
,
8895 if (p
->family
== AF_FLOWSPEC
) {
8896 char retstr
[BGP_FLOWSPEC_STRING_DISPLAY_MAX
];
8898 bgp_fs_nlri_get_string((unsigned char *)
8899 p
->u
.prefix_flowspec
.ptr
,
8900 p
->u
.prefix_flowspec
8903 NLRI_STRING_FORMAT_MIN
,
8906 vty_out(vty
, "\"%s/%d\": ",
8908 p
->u
.prefix_flowspec
.prefixlen
);
8910 vty_out(vty
, ",\"%s/%d\": ",
8912 p
->u
.prefix_flowspec
.prefixlen
);
8914 prefix2str(p
, buf2
, sizeof(buf2
));
8916 vty_out(vty
, "\"%s\": ", buf2
);
8918 vty_out(vty
, ",\"%s\": ", buf2
);
8921 json_object_to_json_string(json_paths
));
8922 json_object_free(json_paths
);
8929 output_count
+= *output_cum
;
8930 *output_cum
= output_count
;
8933 total_count
+= *total_cum
;
8934 *total_cum
= total_count
;
8938 vty_out(vty
, " }%s ", (is_last
? "" : ","));
8942 for (i
= 0; i
< *json_header_depth
; ++i
)
8943 vty_out(vty
, " } ");
8947 /* No route is displayed */
8948 if (output_count
== 0) {
8949 if (type
== bgp_show_type_normal
)
8951 "No BGP prefixes displayed, %ld exist\n",
8955 "\nDisplayed %ld routes and %ld total paths\n",
8956 output_count
, total_count
);
8963 int bgp_show_table_rd(struct vty
*vty
, struct bgp
*bgp
, safi_t safi
,
8964 struct bgp_table
*table
, struct prefix_rd
*prd_match
,
8965 enum bgp_show_type type
, void *output_arg
, bool use_json
)
8967 struct bgp_node
*rn
, *next
;
8968 unsigned long output_cum
= 0;
8969 unsigned long total_cum
= 0;
8970 unsigned long json_header_depth
= 0;
8971 struct bgp_table
*itable
;
8974 show_msg
= (!use_json
&& type
== bgp_show_type_normal
);
8976 for (rn
= bgp_table_top(table
); rn
; rn
= next
) {
8977 next
= bgp_route_next(rn
);
8978 if (prd_match
&& memcmp(rn
->p
.u
.val
, prd_match
->val
, 8) != 0)
8981 itable
= bgp_node_get_bgp_table_info(rn
);
8982 if (itable
!= NULL
) {
8983 struct prefix_rd prd
;
8984 char rd
[RD_ADDRSTRLEN
];
8986 memcpy(&prd
, &(rn
->p
), sizeof(struct prefix_rd
));
8987 prefix_rd2str(&prd
, rd
, sizeof(rd
));
8988 bgp_show_table(vty
, bgp
, safi
, itable
, type
, output_arg
,
8989 use_json
, rd
, next
== NULL
, &output_cum
,
8990 &total_cum
, &json_header_depth
);
8996 if (output_cum
== 0)
8997 vty_out(vty
, "No BGP prefixes displayed, %ld exist\n",
9001 "\nDisplayed %ld routes and %ld total paths\n",
9002 output_cum
, total_cum
);
9006 static int bgp_show(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
, safi_t safi
,
9007 enum bgp_show_type type
, void *output_arg
, bool use_json
)
9009 struct bgp_table
*table
;
9010 unsigned long json_header_depth
= 0;
9013 bgp
= bgp_get_default();
9018 vty_out(vty
, "No BGP process is configured\n");
9020 vty_out(vty
, "{}\n");
9024 table
= bgp
->rib
[afi
][safi
];
9025 /* use MPLS and ENCAP specific shows until they are merged */
9026 if (safi
== SAFI_MPLS_VPN
) {
9027 return bgp_show_table_rd(vty
, bgp
, safi
, table
, NULL
, type
,
9028 output_arg
, use_json
);
9031 if (safi
== SAFI_FLOWSPEC
&& type
== bgp_show_type_detail
) {
9032 return bgp_show_table_flowspec(vty
, bgp
, afi
, table
, type
,
9033 output_arg
, use_json
,
9036 /* labeled-unicast routes live in the unicast table */
9037 else if (safi
== SAFI_LABELED_UNICAST
)
9038 safi
= SAFI_UNICAST
;
9040 return bgp_show_table(vty
, bgp
, safi
, table
, type
, output_arg
, use_json
,
9041 NULL
, 1, NULL
, NULL
, &json_header_depth
);
9044 static void bgp_show_all_instances_routes_vty(struct vty
*vty
, afi_t afi
,
9045 safi_t safi
, bool use_json
)
9047 struct listnode
*node
, *nnode
;
9050 bool route_output
= false;
9053 vty_out(vty
, "{\n");
9055 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
9056 route_output
= true;
9059 vty_out(vty
, ",\n");
9063 vty_out(vty
, "\"%s\":",
9064 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
9068 vty_out(vty
, "\nInstance %s:\n",
9069 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
9073 bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_normal
, NULL
,
9078 vty_out(vty
, "}\n");
9079 else if (!route_output
)
9080 vty_out(vty
, "%% BGP instance not found\n");
9083 /* Header of detailed BGP route information */
9084 void route_vty_out_detail_header(struct vty
*vty
, struct bgp
*bgp
,
9085 struct bgp_node
*rn
, struct prefix_rd
*prd
,
9086 afi_t afi
, safi_t safi
, json_object
*json
)
9088 struct bgp_path_info
*pi
;
9091 struct listnode
*node
, *nnode
;
9092 char buf1
[RD_ADDRSTRLEN
];
9093 char buf2
[INET6_ADDRSTRLEN
];
9094 char buf3
[EVPN_ROUTE_STRLEN
];
9095 char prefix_str
[BUFSIZ
];
9100 int route_filter_translated_v4
= 0;
9101 int route_filter_v4
= 0;
9102 int route_filter_translated_v6
= 0;
9103 int route_filter_v6
= 0;
9106 int accept_own_nexthop
= 0;
9109 int no_advertise
= 0;
9113 int has_valid_label
= 0;
9114 mpls_label_t label
= 0;
9115 json_object
*json_adv_to
= NULL
;
9118 has_valid_label
= bgp_is_valid_label(&rn
->local_label
);
9120 if (has_valid_label
)
9121 label
= label_pton(&rn
->local_label
);
9124 if (has_valid_label
)
9125 json_object_int_add(json
, "localLabel", label
);
9127 json_object_string_add(
9129 prefix2str(p
, prefix_str
, sizeof(prefix_str
)));
9131 if (safi
== SAFI_EVPN
)
9132 vty_out(vty
, "BGP routing table entry for %s%s%s\n",
9133 prd
? prefix_rd2str(prd
, buf1
, sizeof(buf1
))
9136 bgp_evpn_route2str((struct prefix_evpn
*)p
,
9137 buf3
, sizeof(buf3
)));
9139 vty_out(vty
, "BGP routing table entry for %s%s%s/%d\n",
9140 ((safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
)
9141 ? prefix_rd2str(prd
, buf1
,
9144 safi
== SAFI_MPLS_VPN
? ":" : "",
9145 inet_ntop(p
->family
, &p
->u
.prefix
, buf2
,
9149 if (has_valid_label
)
9150 vty_out(vty
, "Local label: %d\n", label
);
9151 if (bgp_labeled_safi(safi
) && safi
!= SAFI_EVPN
)
9152 vty_out(vty
, "not allocated\n");
9155 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
9157 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
9159 if (pi
->extra
&& pi
->extra
->suppress
)
9162 if (pi
->attr
->community
== NULL
)
9165 no_advertise
+= community_include(
9166 pi
->attr
->community
, COMMUNITY_NO_ADVERTISE
);
9167 no_export
+= community_include(pi
->attr
->community
,
9168 COMMUNITY_NO_EXPORT
);
9169 local_as
+= community_include(pi
->attr
->community
,
9170 COMMUNITY_LOCAL_AS
);
9171 accept_own
+= community_include(pi
->attr
->community
,
9172 COMMUNITY_ACCEPT_OWN
);
9173 route_filter_translated_v4
+= community_include(
9174 pi
->attr
->community
,
9175 COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
);
9176 route_filter_translated_v6
+= community_include(
9177 pi
->attr
->community
,
9178 COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
);
9179 route_filter_v4
+= community_include(
9180 pi
->attr
->community
, COMMUNITY_ROUTE_FILTER_v4
);
9181 route_filter_v6
+= community_include(
9182 pi
->attr
->community
, COMMUNITY_ROUTE_FILTER_v6
);
9183 llgr_stale
+= community_include(pi
->attr
->community
,
9184 COMMUNITY_LLGR_STALE
);
9185 no_llgr
+= community_include(pi
->attr
->community
,
9187 accept_own_nexthop
+=
9188 community_include(pi
->attr
->community
,
9189 COMMUNITY_ACCEPT_OWN_NEXTHOP
);
9190 blackhole
+= community_include(pi
->attr
->community
,
9191 COMMUNITY_BLACKHOLE
);
9192 no_peer
+= community_include(pi
->attr
->community
,
9198 vty_out(vty
, "Paths: (%d available", count
);
9200 vty_out(vty
, ", best #%d", best
);
9201 if (safi
== SAFI_UNICAST
)
9202 vty_out(vty
, ", table %s",
9204 == BGP_INSTANCE_TYPE_DEFAULT
)
9208 vty_out(vty
, ", no best path");
9212 ", accept own local route exported and imported in different VRF");
9213 else if (route_filter_translated_v4
)
9215 ", mark translated RTs for VPNv4 route filtering");
9216 else if (route_filter_v4
)
9218 ", attach RT as-is for VPNv4 route filtering");
9219 else if (route_filter_translated_v6
)
9221 ", mark translated RTs for VPNv6 route filtering");
9222 else if (route_filter_v6
)
9224 ", attach RT as-is for VPNv6 route filtering");
9225 else if (llgr_stale
)
9227 ", mark routes to be retained for a longer time. Requeres support for Long-lived BGP Graceful Restart");
9230 ", mark routes to not be treated according to Long-lived BGP Graceful Restart operations");
9231 else if (accept_own_nexthop
)
9233 ", accept local nexthop");
9235 vty_out(vty
, ", inform peer to blackhole prefix");
9237 vty_out(vty
, ", not advertised to EBGP peer");
9238 else if (no_advertise
)
9239 vty_out(vty
, ", not advertised to any peer");
9241 vty_out(vty
, ", not advertised outside local AS");
9244 ", inform EBGP peer not to advertise to their EBGP peers");
9248 ", Advertisements suppressed by an aggregate.");
9249 vty_out(vty
, ")\n");
9252 /* If we are not using addpath then we can display Advertised to and
9254 * show what peers we advertised the bestpath to. If we are using
9256 * though then we must display Advertised to on a path-by-path basis. */
9257 if (!bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
9258 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
9259 if (bgp_adj_out_lookup(peer
, rn
, 0)) {
9260 if (json
&& !json_adv_to
)
9261 json_adv_to
= json_object_new_object();
9263 route_vty_out_advertised_to(
9265 " Advertised to non peer-group peers:\n ",
9272 json_object_object_add(json
, "advertisedTo",
9277 vty_out(vty
, " Not advertised to any peer");
9283 /* Display specified route of BGP table. */
9284 static int bgp_show_route_in_table(struct vty
*vty
, struct bgp
*bgp
,
9285 struct bgp_table
*rib
, const char *ip_str
,
9286 afi_t afi
, safi_t safi
,
9287 struct prefix_rd
*prd
, int prefix_check
,
9288 enum bgp_path_type pathtype
, bool use_json
)
9293 struct prefix match
;
9294 struct bgp_node
*rn
;
9295 struct bgp_node
*rm
;
9296 struct bgp_path_info
*pi
;
9297 struct bgp_table
*table
;
9298 json_object
*json
= NULL
;
9299 json_object
*json_paths
= NULL
;
9301 /* Check IP address argument. */
9302 ret
= str2prefix(ip_str
, &match
);
9304 vty_out(vty
, "address is malformed\n");
9308 match
.family
= afi2family(afi
);
9311 json
= json_object_new_object();
9312 json_paths
= json_object_new_array();
9315 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
|| safi
== SAFI_EVPN
) {
9316 for (rn
= bgp_table_top(rib
); rn
; rn
= bgp_route_next(rn
)) {
9317 if (prd
&& memcmp(rn
->p
.u
.val
, prd
->val
, 8) != 0)
9319 table
= bgp_node_get_bgp_table_info(rn
);
9325 if ((rm
= bgp_node_match(table
, &match
)) == NULL
)
9329 && rm
->p
.prefixlen
!= match
.prefixlen
) {
9330 bgp_unlock_node(rm
);
9334 for (pi
= bgp_node_get_bgp_path_info(rm
); pi
;
9337 route_vty_out_detail_header(
9339 (struct prefix_rd
*)&rn
->p
,
9340 AFI_IP
, safi
, json
);
9345 if (pathtype
== BGP_PATH_SHOW_ALL
9346 || (pathtype
== BGP_PATH_SHOW_BESTPATH
9347 && CHECK_FLAG(pi
->flags
,
9349 || (pathtype
== BGP_PATH_SHOW_MULTIPATH
9350 && (CHECK_FLAG(pi
->flags
,
9352 || CHECK_FLAG(pi
->flags
,
9353 BGP_PATH_SELECTED
))))
9354 route_vty_out_detail(vty
, bgp
, &rm
->p
,
9359 bgp_unlock_node(rm
);
9361 } else if (safi
== SAFI_FLOWSPEC
) {
9362 display
= bgp_flowspec_display_match_per_ip(afi
, rib
,
9363 &match
, prefix_check
,
9370 if ((rn
= bgp_node_match(rib
, &match
)) != NULL
) {
9372 || rn
->p
.prefixlen
== match
.prefixlen
) {
9373 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
9376 route_vty_out_detail_header(
9377 vty
, bgp
, rn
, NULL
, afi
,
9383 if (pathtype
== BGP_PATH_SHOW_ALL
9385 == BGP_PATH_SHOW_BESTPATH
9390 == BGP_PATH_SHOW_MULTIPATH
9396 BGP_PATH_SELECTED
))))
9397 route_vty_out_detail(
9398 vty
, bgp
, &rn
->p
, pi
,
9399 afi
, safi
, json_paths
);
9403 bgp_unlock_node(rn
);
9409 json_object_object_add(json
, "paths", json_paths
);
9411 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
9412 json
, JSON_C_TO_STRING_PRETTY
));
9413 json_object_free(json
);
9416 vty_out(vty
, "%% Network not in table\n");
9424 /* Display specified route of Main RIB */
9425 static int bgp_show_route(struct vty
*vty
, struct bgp
*bgp
, const char *ip_str
,
9426 afi_t afi
, safi_t safi
, struct prefix_rd
*prd
,
9427 int prefix_check
, enum bgp_path_type pathtype
,
9431 bgp
= bgp_get_default();
9434 vty_out(vty
, "No BGP process is configured\n");
9436 vty_out(vty
, "{}\n");
9441 /* labeled-unicast routes live in the unicast table */
9442 if (safi
== SAFI_LABELED_UNICAST
)
9443 safi
= SAFI_UNICAST
;
9445 return bgp_show_route_in_table(vty
, bgp
, bgp
->rib
[afi
][safi
], ip_str
,
9446 afi
, safi
, prd
, prefix_check
, pathtype
,
9450 static int bgp_show_lcommunity(struct vty
*vty
, struct bgp
*bgp
, int argc
,
9451 struct cmd_token
**argv
, afi_t afi
, safi_t safi
,
9454 struct lcommunity
*lcom
;
9460 b
= buffer_new(1024);
9461 for (i
= 0; i
< argc
; i
++) {
9463 buffer_putc(b
, ' ');
9465 if (strmatch(argv
[i
]->text
, "AA:BB:CC")) {
9467 buffer_putstr(b
, argv
[i
]->arg
);
9471 buffer_putc(b
, '\0');
9473 str
= buffer_getstr(b
);
9476 lcom
= lcommunity_str2com(str
);
9477 XFREE(MTYPE_TMP
, str
);
9479 vty_out(vty
, "%% Large-community malformed\n");
9483 return bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_lcommunity
, lcom
,
9487 static int bgp_show_lcommunity_list(struct vty
*vty
, struct bgp
*bgp
,
9488 const char *lcom
, afi_t afi
, safi_t safi
,
9491 struct community_list
*list
;
9493 list
= community_list_lookup(bgp_clist
, lcom
, 0,
9494 LARGE_COMMUNITY_LIST_MASTER
);
9496 vty_out(vty
, "%% %s is not a valid large-community-list name\n",
9501 return bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_lcommunity_list
,
9505 DEFUN (show_ip_bgp_large_community_list
,
9506 show_ip_bgp_large_community_list_cmd
,
9507 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] large-community-list <(1-500)|WORD> [json]",
9511 BGP_INSTANCE_HELP_STR
9513 BGP_SAFI_WITH_LABEL_HELP_STR
9514 "Display routes matching the large-community-list\n"
9515 "large-community-list number\n"
9516 "large-community-list name\n"
9520 afi_t afi
= AFI_IP6
;
9521 safi_t safi
= SAFI_UNICAST
;
9524 if (argv_find(argv
, argc
, "ip", &idx
))
9526 if (argv_find(argv
, argc
, "view", &idx
)
9527 || argv_find(argv
, argc
, "vrf", &idx
))
9528 vrf
= argv
[++idx
]->arg
;
9529 if (argv_find(argv
, argc
, "ipv4", &idx
)
9530 || argv_find(argv
, argc
, "ipv6", &idx
)) {
9531 afi
= strmatch(argv
[idx
]->text
, "ipv6") ? AFI_IP6
: AFI_IP
;
9532 if (argv_find(argv
, argc
, "unicast", &idx
)
9533 || argv_find(argv
, argc
, "multicast", &idx
))
9534 safi
= bgp_vty_safi_from_str(argv
[idx
]->text
);
9537 bool uj
= use_json(argc
, argv
);
9539 struct bgp
*bgp
= bgp_lookup_by_name(vrf
);
9541 vty_out(vty
, "Can't find BGP instance %s\n", vrf
);
9545 argv_find(argv
, argc
, "large-community-list", &idx
);
9546 return bgp_show_lcommunity_list(vty
, bgp
, argv
[idx
+ 1]->arg
, afi
, safi
,
9549 DEFUN (show_ip_bgp_large_community
,
9550 show_ip_bgp_large_community_cmd
,
9551 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] large-community [AA:BB:CC] [json]",
9555 BGP_INSTANCE_HELP_STR
9557 BGP_SAFI_WITH_LABEL_HELP_STR
9558 "Display routes matching the large-communities\n"
9559 "List of large-community numbers\n"
9563 afi_t afi
= AFI_IP6
;
9564 safi_t safi
= SAFI_UNICAST
;
9567 if (argv_find(argv
, argc
, "ip", &idx
))
9569 if (argv_find(argv
, argc
, "view", &idx
)
9570 || argv_find(argv
, argc
, "vrf", &idx
))
9571 vrf
= argv
[++idx
]->arg
;
9572 if (argv_find(argv
, argc
, "ipv4", &idx
)
9573 || argv_find(argv
, argc
, "ipv6", &idx
)) {
9574 afi
= strmatch(argv
[idx
]->text
, "ipv6") ? AFI_IP6
: AFI_IP
;
9575 if (argv_find(argv
, argc
, "unicast", &idx
)
9576 || argv_find(argv
, argc
, "multicast", &idx
))
9577 safi
= bgp_vty_safi_from_str(argv
[idx
]->text
);
9580 bool uj
= use_json(argc
, argv
);
9582 struct bgp
*bgp
= bgp_lookup_by_name(vrf
);
9584 vty_out(vty
, "Can't find BGP instance %s\n", vrf
);
9588 if (argv_find(argv
, argc
, "AA:BB:CC", &idx
))
9589 return bgp_show_lcommunity(vty
, bgp
, argc
, argv
, afi
, safi
, uj
);
9591 return bgp_show(vty
, bgp
, afi
, safi
,
9592 bgp_show_type_lcommunity_all
, NULL
, uj
);
9595 static int bgp_table_stats(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
9599 /* BGP route print out function without JSON */
9602 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]]\
9603 <dampening <parameters>\
9608 |community-list <(1-500)|WORD> [exact-match]\
9609 |A.B.C.D/M longer-prefixes\
9610 |X:X::X:X/M longer-prefixes\
9615 BGP_INSTANCE_HELP_STR
9617 BGP_SAFI_WITH_LABEL_HELP_STR
9618 "Display detailed information about dampening\n"
9619 "Display detail of configured dampening parameters\n"
9620 "Display routes matching the route-map\n"
9621 "A route-map to match on\n"
9622 "Display routes conforming to the prefix-list\n"
9623 "Prefix-list name\n"
9624 "Display routes conforming to the filter-list\n"
9625 "Regular expression access list name\n"
9626 "BGP RIB advertisement statistics\n"
9627 "Display routes matching the community-list\n"
9628 "community-list number\n"
9629 "community-list name\n"
9630 "Exact match of the communities\n"
9632 "Display route and more specific routes\n"
9634 "Display route and more specific routes\n")
9636 afi_t afi
= AFI_IP6
;
9637 safi_t safi
= SAFI_UNICAST
;
9638 int exact_match
= 0;
9639 struct bgp
*bgp
= NULL
;
9642 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
9647 if (argv_find(argv
, argc
, "dampening", &idx
)) {
9648 if (argv_find(argv
, argc
, "parameters", &idx
))
9649 return bgp_show_dampening_parameters(vty
, afi
, safi
);
9652 if (argv_find(argv
, argc
, "prefix-list", &idx
))
9653 return bgp_show_prefix_list(vty
, bgp
, argv
[idx
+ 1]->arg
, afi
,
9654 safi
, bgp_show_type_prefix_list
);
9656 if (argv_find(argv
, argc
, "filter-list", &idx
))
9657 return bgp_show_filter_list(vty
, bgp
, argv
[idx
+ 1]->arg
, afi
,
9658 safi
, bgp_show_type_filter_list
);
9660 if (argv_find(argv
, argc
, "statistics", &idx
))
9661 return bgp_table_stats(vty
, bgp
, afi
, safi
);
9663 if (argv_find(argv
, argc
, "route-map", &idx
))
9664 return bgp_show_route_map(vty
, bgp
, argv
[idx
+ 1]->arg
, afi
,
9665 safi
, bgp_show_type_route_map
);
9667 if (argv_find(argv
, argc
, "community-list", &idx
)) {
9668 const char *clist_number_or_name
= argv
[++idx
]->arg
;
9669 if (++idx
< argc
&& strmatch(argv
[idx
]->text
, "exact-match"))
9671 return bgp_show_community_list(vty
, bgp
, clist_number_or_name
,
9672 exact_match
, afi
, safi
);
9675 if (argv_find(argv
, argc
, "A.B.C.D/M", &idx
)
9676 || argv_find(argv
, argc
, "X:X::X:X/M", &idx
))
9677 return bgp_show_prefix_longer(vty
, bgp
, argv
[idx
]->arg
, afi
,
9679 bgp_show_type_prefix_longer
);
9684 /* BGP route print out function with JSON */
9685 DEFUN (show_ip_bgp_json
,
9686 show_ip_bgp_json_cmd
,
9687 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]]\
9689 |dampening <flap-statistics|dampened-paths>\
9690 |community [AA:NN|local-AS|no-advertise|no-export\
9691 |graceful-shutdown|no-peer|blackhole|llgr-stale|no-llgr\
9692 |accept-own|accept-own-nexthop|route-filter-v6\
9693 |route-filter-v4|route-filter-translated-v6\
9694 |route-filter-translated-v4] [exact-match]\
9699 BGP_INSTANCE_HELP_STR
9701 BGP_SAFI_WITH_LABEL_HELP_STR
9702 "Display only routes with non-natural netmasks\n"
9703 "Display detailed information about dampening\n"
9704 "Display flap statistics of routes\n"
9705 "Display paths suppressed due to dampening\n"
9706 "Display routes matching the communities\n"
9708 "Do not send outside local AS (well-known community)\n"
9709 "Do not advertise to any peer (well-known community)\n"
9710 "Do not export to next AS (well-known community)\n"
9711 "Graceful shutdown (well-known community)\n"
9712 "Do not export to any peer (well-known community)\n"
9713 "Inform EBGP peers to blackhole traffic to prefix (well-known community)\n"
9714 "Staled Long-lived Graceful Restart VPN route (well-known community)\n"
9715 "Removed because Long-lived Graceful Restart was not enabled for VPN route (well-known community)\n"
9716 "Should accept local VPN route if exported and imported into different VRF (well-known community)\n"
9717 "Should accept VPN route with local nexthop (well-known community)\n"
9718 "RT VPNv6 route filtering (well-known community)\n"
9719 "RT VPNv4 route filtering (well-known community)\n"
9720 "RT translated VPNv6 route filtering (well-known community)\n"
9721 "RT translated VPNv4 route filtering (well-known community)\n"
9722 "Exact match of the communities\n"
9725 afi_t afi
= AFI_IP6
;
9726 safi_t safi
= SAFI_UNICAST
;
9727 enum bgp_show_type sh_type
= bgp_show_type_normal
;
9728 struct bgp
*bgp
= NULL
;
9730 int exact_match
= 0;
9731 bool uj
= use_json(argc
, argv
);
9736 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
9741 if (argv_find(argv
, argc
, "cidr-only", &idx
))
9742 return bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_cidr_only
,
9745 if (argv_find(argv
, argc
, "dampening", &idx
)) {
9746 if (argv_find(argv
, argc
, "dampened-paths", &idx
))
9747 return bgp_show(vty
, bgp
, afi
, safi
,
9748 bgp_show_type_dampend_paths
, NULL
, uj
);
9749 else if (argv_find(argv
, argc
, "flap-statistics", &idx
))
9750 return bgp_show(vty
, bgp
, afi
, safi
,
9751 bgp_show_type_flap_statistics
, NULL
,
9755 if (argv_find(argv
, argc
, "community", &idx
)) {
9756 char *maybecomm
= NULL
;
9757 char *community
= NULL
;
9759 if (idx
+ 1 < argc
) {
9760 if (argv
[idx
+ 1]->type
== VARIABLE_TKN
)
9761 maybecomm
= argv
[idx
+ 1]->arg
;
9763 maybecomm
= argv
[idx
+ 1]->text
;
9766 if (maybecomm
&& !strmatch(maybecomm
, "json")
9767 && !strmatch(maybecomm
, "exact-match"))
9768 community
= maybecomm
;
9770 if (argv_find(argv
, argc
, "exact-match", &idx
))
9774 return bgp_show_community(vty
, bgp
, community
,
9775 exact_match
, afi
, safi
, uj
);
9777 return (bgp_show(vty
, bgp
, afi
, safi
,
9778 bgp_show_type_community_all
, NULL
,
9782 return bgp_show(vty
, bgp
, afi
, safi
, sh_type
, NULL
, uj
);
9785 DEFUN (show_ip_bgp_route
,
9786 show_ip_bgp_route_cmd
,
9787 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]]"
9788 "<A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> [<bestpath|multipath>] [json]",
9792 BGP_INSTANCE_HELP_STR
9794 BGP_SAFI_WITH_LABEL_HELP_STR
9795 "Network in the BGP routing table to display\n"
9797 "Network in the BGP routing table to display\n"
9799 "Display only the bestpath\n"
9800 "Display only multipaths\n"
9803 int prefix_check
= 0;
9805 afi_t afi
= AFI_IP6
;
9806 safi_t safi
= SAFI_UNICAST
;
9807 char *prefix
= NULL
;
9808 struct bgp
*bgp
= NULL
;
9809 enum bgp_path_type path_type
;
9810 bool uj
= use_json(argc
, argv
);
9814 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
9821 "Specified 'all' vrf's but this command currently only works per view/vrf\n");
9825 /* <A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> */
9826 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)
9827 || argv_find(argv
, argc
, "X:X::X:X", &idx
))
9829 else if (argv_find(argv
, argc
, "A.B.C.D/M", &idx
)
9830 || argv_find(argv
, argc
, "X:X::X:X/M", &idx
))
9833 if ((argv
[idx
]->type
== IPV6_TKN
|| argv
[idx
]->type
== IPV6_PREFIX_TKN
)
9834 && afi
!= AFI_IP6
) {
9836 "%% Cannot specify IPv6 address or prefix with IPv4 AFI\n");
9839 if ((argv
[idx
]->type
== IPV4_TKN
|| argv
[idx
]->type
== IPV4_PREFIX_TKN
)
9842 "%% Cannot specify IPv4 address or prefix with IPv6 AFI\n");
9846 prefix
= argv
[idx
]->arg
;
9848 /* [<bestpath|multipath>] */
9849 if (argv_find(argv
, argc
, "bestpath", &idx
))
9850 path_type
= BGP_PATH_SHOW_BESTPATH
;
9851 else if (argv_find(argv
, argc
, "multipath", &idx
))
9852 path_type
= BGP_PATH_SHOW_MULTIPATH
;
9854 path_type
= BGP_PATH_SHOW_ALL
;
9856 return bgp_show_route(vty
, bgp
, prefix
, afi
, safi
, NULL
, prefix_check
,
9860 DEFUN (show_ip_bgp_regexp
,
9861 show_ip_bgp_regexp_cmd
,
9862 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] regexp REGEX...",
9866 BGP_INSTANCE_HELP_STR
9868 BGP_SAFI_WITH_LABEL_HELP_STR
9869 "Display routes matching the AS path regular expression\n"
9870 "A regular-expression to match the BGP AS paths\n")
9872 afi_t afi
= AFI_IP6
;
9873 safi_t safi
= SAFI_UNICAST
;
9874 struct bgp
*bgp
= NULL
;
9877 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
9882 // get index of regex
9883 argv_find(argv
, argc
, "regexp", &idx
);
9886 char *regstr
= argv_concat(argv
, argc
, idx
);
9887 int rc
= bgp_show_regexp(vty
, bgp
, (const char *)regstr
, afi
, safi
,
9888 bgp_show_type_regexp
);
9889 XFREE(MTYPE_TMP
, regstr
);
9893 DEFUN (show_ip_bgp_instance_all
,
9894 show_ip_bgp_instance_all_cmd
,
9895 "show [ip] bgp <view|vrf> all ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] [json]",
9899 BGP_INSTANCE_ALL_HELP_STR
9901 BGP_SAFI_WITH_LABEL_HELP_STR
9905 safi_t safi
= SAFI_UNICAST
;
9906 struct bgp
*bgp
= NULL
;
9908 bool uj
= use_json(argc
, argv
);
9913 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
9918 bgp_show_all_instances_routes_vty(vty
, afi
, safi
, uj
);
9922 static int bgp_show_regexp(struct vty
*vty
, struct bgp
*bgp
, const char *regstr
,
9923 afi_t afi
, safi_t safi
, enum bgp_show_type type
)
9928 regex
= bgp_regcomp(regstr
);
9930 vty_out(vty
, "Can't compile regexp %s\n", regstr
);
9934 rc
= bgp_show(vty
, bgp
, afi
, safi
, type
, regex
, 0);
9935 bgp_regex_free(regex
);
9939 static int bgp_show_prefix_list(struct vty
*vty
, struct bgp
*bgp
,
9940 const char *prefix_list_str
, afi_t afi
,
9941 safi_t safi
, enum bgp_show_type type
)
9943 struct prefix_list
*plist
;
9945 plist
= prefix_list_lookup(afi
, prefix_list_str
);
9946 if (plist
== NULL
) {
9947 vty_out(vty
, "%% %s is not a valid prefix-list name\n",
9952 return bgp_show(vty
, bgp
, afi
, safi
, type
, plist
, 0);
9955 static int bgp_show_filter_list(struct vty
*vty
, struct bgp
*bgp
,
9956 const char *filter
, afi_t afi
, safi_t safi
,
9957 enum bgp_show_type type
)
9959 struct as_list
*as_list
;
9961 as_list
= as_list_lookup(filter
);
9962 if (as_list
== NULL
) {
9963 vty_out(vty
, "%% %s is not a valid AS-path access-list name\n",
9968 return bgp_show(vty
, bgp
, afi
, safi
, type
, as_list
, 0);
9971 static int bgp_show_route_map(struct vty
*vty
, struct bgp
*bgp
,
9972 const char *rmap_str
, afi_t afi
, safi_t safi
,
9973 enum bgp_show_type type
)
9975 struct route_map
*rmap
;
9977 rmap
= route_map_lookup_by_name(rmap_str
);
9979 vty_out(vty
, "%% %s is not a valid route-map name\n", rmap_str
);
9983 return bgp_show(vty
, bgp
, afi
, safi
, type
, rmap
, 0);
9986 static int bgp_show_community(struct vty
*vty
, struct bgp
*bgp
,
9987 const char *comstr
, int exact
, afi_t afi
,
9988 safi_t safi
, bool use_json
)
9990 struct community
*com
;
9993 com
= community_str2com(comstr
);
9995 vty_out(vty
, "%% Community malformed: %s\n", comstr
);
9999 ret
= bgp_show(vty
, bgp
, afi
, safi
,
10000 (exact
? bgp_show_type_community_exact
10001 : bgp_show_type_community
),
10003 community_free(&com
);
10008 static int bgp_show_community_list(struct vty
*vty
, struct bgp
*bgp
,
10009 const char *com
, int exact
, afi_t afi
,
10012 struct community_list
*list
;
10014 list
= community_list_lookup(bgp_clist
, com
, 0, COMMUNITY_LIST_MASTER
);
10015 if (list
== NULL
) {
10016 vty_out(vty
, "%% %s is not a valid community-list name\n", com
);
10017 return CMD_WARNING
;
10020 return bgp_show(vty
, bgp
, afi
, safi
,
10021 (exact
? bgp_show_type_community_list_exact
10022 : bgp_show_type_community_list
),
10026 static int bgp_show_prefix_longer(struct vty
*vty
, struct bgp
*bgp
,
10027 const char *prefix
, afi_t afi
, safi_t safi
,
10028 enum bgp_show_type type
)
10035 ret
= str2prefix(prefix
, p
);
10037 vty_out(vty
, "%% Malformed Prefix\n");
10038 return CMD_WARNING
;
10041 ret
= bgp_show(vty
, bgp
, afi
, safi
, type
, p
, 0);
10046 static struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
10047 const char *ip_str
, bool use_json
)
10051 union sockunion su
;
10053 /* Get peer sockunion. */
10054 ret
= str2sockunion(ip_str
, &su
);
10056 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
10058 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
10062 json_object
*json_no
= NULL
;
10063 json_no
= json_object_new_object();
10064 json_object_string_add(
10066 "malformedAddressOrName",
10068 vty_out(vty
, "%s\n",
10069 json_object_to_json_string_ext(
10071 JSON_C_TO_STRING_PRETTY
));
10072 json_object_free(json_no
);
10075 "%% Malformed address or name: %s\n",
10083 /* Peer structure lookup. */
10084 peer
= peer_lookup(bgp
, &su
);
10087 json_object
*json_no
= NULL
;
10088 json_no
= json_object_new_object();
10089 json_object_string_add(json_no
, "warning",
10090 "No such neighbor in this view/vrf");
10091 vty_out(vty
, "%s\n",
10092 json_object_to_json_string_ext(
10093 json_no
, JSON_C_TO_STRING_PRETTY
));
10094 json_object_free(json_no
);
10096 vty_out(vty
, "No such neighbor in this view/vrf\n");
10104 BGP_STATS_MAXBITLEN
= 0,
10106 BGP_STATS_PREFIXES
,
10108 BGP_STATS_UNAGGREGATEABLE
,
10109 BGP_STATS_MAX_AGGREGATEABLE
,
10110 BGP_STATS_AGGREGATES
,
10112 BGP_STATS_ASPATH_COUNT
,
10113 BGP_STATS_ASPATH_MAXHOPS
,
10114 BGP_STATS_ASPATH_TOTHOPS
,
10115 BGP_STATS_ASPATH_MAXSIZE
,
10116 BGP_STATS_ASPATH_TOTSIZE
,
10117 BGP_STATS_ASN_HIGHEST
,
10121 static const char *table_stats_strs
[] = {
10122 [BGP_STATS_PREFIXES
] = "Total Prefixes",
10123 [BGP_STATS_TOTPLEN
] = "Average prefix length",
10124 [BGP_STATS_RIB
] = "Total Advertisements",
10125 [BGP_STATS_UNAGGREGATEABLE
] = "Unaggregateable prefixes",
10126 [BGP_STATS_MAX_AGGREGATEABLE
] =
10127 "Maximum aggregateable prefixes",
10128 [BGP_STATS_AGGREGATES
] = "BGP Aggregate advertisements",
10129 [BGP_STATS_SPACE
] = "Address space advertised",
10130 [BGP_STATS_ASPATH_COUNT
] = "Advertisements with paths",
10131 [BGP_STATS_ASPATH_MAXHOPS
] = "Longest AS-Path (hops)",
10132 [BGP_STATS_ASPATH_MAXSIZE
] = "Largest AS-Path (bytes)",
10133 [BGP_STATS_ASPATH_TOTHOPS
] = "Average AS-Path length (hops)",
10134 [BGP_STATS_ASPATH_TOTSIZE
] = "Average AS-Path size (bytes)",
10135 [BGP_STATS_ASN_HIGHEST
] = "Highest public ASN",
10136 [BGP_STATS_MAX
] = NULL
,
10139 struct bgp_table_stats
{
10140 struct bgp_table
*table
;
10141 unsigned long long counts
[BGP_STATS_MAX
];
10142 double total_space
;
10146 #define TALLY_SIGFIG 100000
10147 static unsigned long
10148 ravg_tally (unsigned long count
, unsigned long oldavg
, unsigned long newval
)
10150 unsigned long newtot
= (count
-1) * oldavg
+ (newval
* TALLY_SIGFIG
);
10151 unsigned long res
= (newtot
* TALLY_SIGFIG
) / count
;
10152 unsigned long ret
= newtot
/ count
;
10154 if ((res
% TALLY_SIGFIG
) > (TALLY_SIGFIG
/2))
10161 static int bgp_table_stats_walker(struct thread
*t
)
10163 struct bgp_node
*rn
;
10164 struct bgp_node
*top
;
10165 struct bgp_table_stats
*ts
= THREAD_ARG(t
);
10166 unsigned int space
= 0;
10168 if (!(top
= bgp_table_top(ts
->table
)))
10171 switch (top
->p
.family
) {
10173 space
= IPV4_MAX_BITLEN
;
10176 space
= IPV6_MAX_BITLEN
;
10180 ts
->counts
[BGP_STATS_MAXBITLEN
] = space
;
10182 for (rn
= top
; rn
; rn
= bgp_route_next(rn
)) {
10183 struct bgp_path_info
*pi
;
10184 struct bgp_node
*prn
= bgp_node_parent_nolock(rn
);
10185 unsigned int pinum
= 0;
10190 if (!bgp_node_has_bgp_path_info_data(rn
))
10193 ts
->counts
[BGP_STATS_PREFIXES
]++;
10194 ts
->counts
[BGP_STATS_TOTPLEN
] += rn
->p
.prefixlen
;
10197 ts
->counts
[BGP_STATS_AVGPLEN
]
10198 = ravg_tally (ts
->counts
[BGP_STATS_PREFIXES
],
10199 ts
->counts
[BGP_STATS_AVGPLEN
],
10203 /* check if the prefix is included by any other announcements */
10204 while (prn
&& !bgp_node_has_bgp_path_info_data(prn
))
10205 prn
= bgp_node_parent_nolock(prn
);
10207 if (prn
== NULL
|| prn
== top
) {
10208 ts
->counts
[BGP_STATS_UNAGGREGATEABLE
]++;
10209 /* announced address space */
10212 pow(2.0, space
- rn
->p
.prefixlen
);
10213 } else if (bgp_node_has_bgp_path_info_data(prn
))
10214 ts
->counts
[BGP_STATS_MAX_AGGREGATEABLE
]++;
10216 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
10218 ts
->counts
[BGP_STATS_RIB
]++;
10221 && (CHECK_FLAG(pi
->attr
->flag
,
10223 BGP_ATTR_ATOMIC_AGGREGATE
))))
10224 ts
->counts
[BGP_STATS_AGGREGATES
]++;
10226 /* as-path stats */
10227 if (pi
->attr
&& pi
->attr
->aspath
) {
10228 unsigned int hops
=
10229 aspath_count_hops(pi
->attr
->aspath
);
10230 unsigned int size
=
10231 aspath_size(pi
->attr
->aspath
);
10232 as_t highest
= aspath_highest(pi
->attr
->aspath
);
10234 ts
->counts
[BGP_STATS_ASPATH_COUNT
]++;
10236 if (hops
> ts
->counts
[BGP_STATS_ASPATH_MAXHOPS
])
10237 ts
->counts
[BGP_STATS_ASPATH_MAXHOPS
] =
10240 if (size
> ts
->counts
[BGP_STATS_ASPATH_MAXSIZE
])
10241 ts
->counts
[BGP_STATS_ASPATH_MAXSIZE
] =
10244 ts
->counts
[BGP_STATS_ASPATH_TOTHOPS
] += hops
;
10245 ts
->counts
[BGP_STATS_ASPATH_TOTSIZE
] += size
;
10247 ts
->counts
[BGP_STATS_ASPATH_AVGHOPS
]
10248 = ravg_tally (ts
->counts
[BGP_STATS_ASPATH_COUNT
],
10249 ts
->counts
[BGP_STATS_ASPATH_AVGHOPS
],
10251 ts
->counts
[BGP_STATS_ASPATH_AVGSIZE
]
10252 = ravg_tally (ts
->counts
[BGP_STATS_ASPATH_COUNT
],
10253 ts
->counts
[BGP_STATS_ASPATH_AVGSIZE
],
10256 if (highest
> ts
->counts
[BGP_STATS_ASN_HIGHEST
])
10257 ts
->counts
[BGP_STATS_ASN_HIGHEST
] =
10265 static int bgp_table_stats(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
10268 struct bgp_table_stats ts
;
10271 if (!bgp
->rib
[afi
][safi
]) {
10272 vty_out(vty
, "%% No RIB exist's for the AFI(%d)/SAFI(%d)\n",
10274 return CMD_WARNING
;
10277 vty_out(vty
, "BGP %s RIB statistics\n", afi_safi_print(afi
, safi
));
10279 /* labeled-unicast routes live in the unicast table */
10280 if (safi
== SAFI_LABELED_UNICAST
)
10281 safi
= SAFI_UNICAST
;
10283 memset(&ts
, 0, sizeof(ts
));
10284 ts
.table
= bgp
->rib
[afi
][safi
];
10285 thread_execute(bm
->master
, bgp_table_stats_walker
, &ts
, 0);
10287 for (i
= 0; i
< BGP_STATS_MAX
; i
++) {
10288 if (!table_stats_strs
[i
])
10293 case BGP_STATS_ASPATH_AVGHOPS
:
10294 case BGP_STATS_ASPATH_AVGSIZE
:
10295 case BGP_STATS_AVGPLEN
:
10296 vty_out (vty
, "%-30s: ", table_stats_strs
[i
]);
10297 vty_out (vty
, "%12.2f",
10298 (float)ts
.counts
[i
] / (float)TALLY_SIGFIG
);
10301 case BGP_STATS_ASPATH_TOTHOPS
:
10302 case BGP_STATS_ASPATH_TOTSIZE
:
10303 vty_out(vty
, "%-30s: ", table_stats_strs
[i
]);
10304 vty_out(vty
, "%12.2f",
10306 ? (float)ts
.counts
[i
]
10308 [BGP_STATS_ASPATH_COUNT
]
10311 case BGP_STATS_TOTPLEN
:
10312 vty_out(vty
, "%-30s: ", table_stats_strs
[i
]);
10313 vty_out(vty
, "%12.2f",
10315 ? (float)ts
.counts
[i
]
10317 [BGP_STATS_PREFIXES
]
10320 case BGP_STATS_SPACE
:
10321 vty_out(vty
, "%-30s: ", table_stats_strs
[i
]);
10322 vty_out(vty
, "%12g\n", ts
.total_space
);
10324 if (afi
== AFI_IP6
) {
10325 vty_out(vty
, "%30s: ", "/32 equivalent ");
10326 vty_out(vty
, "%12g\n",
10327 ts
.total_space
* pow(2.0, -128 + 32));
10328 vty_out(vty
, "%30s: ", "/48 equivalent ");
10329 vty_out(vty
, "%12g\n",
10330 ts
.total_space
* pow(2.0, -128 + 48));
10332 vty_out(vty
, "%30s: ", "% announced ");
10333 vty_out(vty
, "%12.2f\n",
10334 ts
.total_space
* 100. * pow(2.0, -32));
10335 vty_out(vty
, "%30s: ", "/8 equivalent ");
10336 vty_out(vty
, "%12.2f\n",
10337 ts
.total_space
* pow(2.0, -32 + 8));
10338 vty_out(vty
, "%30s: ", "/24 equivalent ");
10339 vty_out(vty
, "%12.2f\n",
10340 ts
.total_space
* pow(2.0, -32 + 24));
10344 vty_out(vty
, "%-30s: ", table_stats_strs
[i
]);
10345 vty_out(vty
, "%12llu", ts
.counts
[i
]);
10348 vty_out(vty
, "\n");
10350 return CMD_SUCCESS
;
10362 PCOUNT_PFCNT
, /* the figure we display to users */
10366 static const char *pcount_strs
[] = {
10367 [PCOUNT_ADJ_IN
] = "Adj-in",
10368 [PCOUNT_DAMPED
] = "Damped",
10369 [PCOUNT_REMOVED
] = "Removed",
10370 [PCOUNT_HISTORY
] = "History",
10371 [PCOUNT_STALE
] = "Stale",
10372 [PCOUNT_VALID
] = "Valid",
10373 [PCOUNT_ALL
] = "All RIB",
10374 [PCOUNT_COUNTED
] = "PfxCt counted",
10375 [PCOUNT_PFCNT
] = "Useable",
10376 [PCOUNT_MAX
] = NULL
,
10379 struct peer_pcounts
{
10380 unsigned int count
[PCOUNT_MAX
];
10381 const struct peer
*peer
;
10382 const struct bgp_table
*table
;
10385 static int bgp_peer_count_walker(struct thread
*t
)
10387 struct bgp_node
*rn
;
10388 struct peer_pcounts
*pc
= THREAD_ARG(t
);
10389 const struct peer
*peer
= pc
->peer
;
10391 for (rn
= bgp_table_top(pc
->table
); rn
; rn
= bgp_route_next(rn
)) {
10392 struct bgp_adj_in
*ain
;
10393 struct bgp_path_info
*pi
;
10395 for (ain
= rn
->adj_in
; ain
; ain
= ain
->next
)
10396 if (ain
->peer
== peer
)
10397 pc
->count
[PCOUNT_ADJ_IN
]++;
10399 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
10401 if (pi
->peer
!= peer
)
10404 pc
->count
[PCOUNT_ALL
]++;
10406 if (CHECK_FLAG(pi
->flags
, BGP_PATH_DAMPED
))
10407 pc
->count
[PCOUNT_DAMPED
]++;
10408 if (CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
))
10409 pc
->count
[PCOUNT_HISTORY
]++;
10410 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
10411 pc
->count
[PCOUNT_REMOVED
]++;
10412 if (CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
))
10413 pc
->count
[PCOUNT_STALE
]++;
10414 if (CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
10415 pc
->count
[PCOUNT_VALID
]++;
10416 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_UNUSEABLE
))
10417 pc
->count
[PCOUNT_PFCNT
]++;
10419 if (CHECK_FLAG(pi
->flags
, BGP_PATH_COUNTED
)) {
10420 pc
->count
[PCOUNT_COUNTED
]++;
10421 if (CHECK_FLAG(pi
->flags
, BGP_PATH_UNUSEABLE
))
10423 EC_LIB_DEVELOPMENT
,
10424 "Attempting to count but flags say it is unusable");
10426 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_UNUSEABLE
))
10428 EC_LIB_DEVELOPMENT
,
10429 "Not counted but flags say we should");
10436 static int bgp_peer_counts(struct vty
*vty
, struct peer
*peer
, afi_t afi
,
10437 safi_t safi
, bool use_json
)
10439 struct peer_pcounts pcounts
= {.peer
= peer
};
10441 json_object
*json
= NULL
;
10442 json_object
*json_loop
= NULL
;
10445 json
= json_object_new_object();
10446 json_loop
= json_object_new_object();
10449 if (!peer
|| !peer
->bgp
|| !peer
->afc
[afi
][safi
]
10450 || !peer
->bgp
->rib
[afi
][safi
]) {
10452 json_object_string_add(
10454 "No such neighbor or address family");
10455 vty_out(vty
, "%s\n", json_object_to_json_string(json
));
10456 json_object_free(json
);
10458 vty_out(vty
, "%% No such neighbor or address family\n");
10460 return CMD_WARNING
;
10463 memset(&pcounts
, 0, sizeof(pcounts
));
10464 pcounts
.peer
= peer
;
10465 pcounts
.table
= peer
->bgp
->rib
[afi
][safi
];
10467 /* in-place call via thread subsystem so as to record execution time
10468 * stats for the thread-walk (i.e. ensure this can't be blamed on
10469 * on just vty_read()).
10471 thread_execute(bm
->master
, bgp_peer_count_walker
, &pcounts
, 0);
10474 json_object_string_add(json
, "prefixCountsFor", peer
->host
);
10475 json_object_string_add(json
, "multiProtocol",
10476 afi_safi_print(afi
, safi
));
10477 json_object_int_add(json
, "pfxCounter",
10478 peer
->pcount
[afi
][safi
]);
10480 for (i
= 0; i
< PCOUNT_MAX
; i
++)
10481 json_object_int_add(json_loop
, pcount_strs
[i
],
10484 json_object_object_add(json
, "ribTableWalkCounters", json_loop
);
10486 if (pcounts
.count
[PCOUNT_PFCNT
] != peer
->pcount
[afi
][safi
]) {
10487 json_object_string_add(json
, "pfxctDriftFor",
10489 json_object_string_add(
10490 json
, "recommended",
10491 "Please report this bug, with the above command output");
10493 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
10494 json
, JSON_C_TO_STRING_PRETTY
));
10495 json_object_free(json
);
10499 && bgp_flag_check(peer
->bgp
, BGP_FLAG_SHOW_HOSTNAME
)) {
10500 vty_out(vty
, "Prefix counts for %s/%s, %s\n",
10501 peer
->hostname
, peer
->host
,
10502 afi_safi_print(afi
, safi
));
10504 vty_out(vty
, "Prefix counts for %s, %s\n", peer
->host
,
10505 afi_safi_print(afi
, safi
));
10508 vty_out(vty
, "PfxCt: %ld\n", peer
->pcount
[afi
][safi
]);
10509 vty_out(vty
, "\nCounts from RIB table walk:\n\n");
10511 for (i
= 0; i
< PCOUNT_MAX
; i
++)
10512 vty_out(vty
, "%20s: %-10d\n", pcount_strs
[i
],
10515 if (pcounts
.count
[PCOUNT_PFCNT
] != peer
->pcount
[afi
][safi
]) {
10516 vty_out(vty
, "%s [pcount] PfxCt drift!\n", peer
->host
);
10518 "Please report this bug, with the above command output\n");
10522 return CMD_SUCCESS
;
10525 DEFUN (show_ip_bgp_instance_neighbor_prefix_counts
,
10526 show_ip_bgp_instance_neighbor_prefix_counts_cmd
,
10527 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_CMD_STR
"]] "
10528 "neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
10532 BGP_INSTANCE_HELP_STR
10535 "Detailed information on TCP and BGP neighbor connections\n"
10536 "Neighbor to display information about\n"
10537 "Neighbor to display information about\n"
10538 "Neighbor on BGP configured interface\n"
10539 "Display detailed prefix count information\n"
10542 afi_t afi
= AFI_IP6
;
10543 safi_t safi
= SAFI_UNICAST
;
10546 struct bgp
*bgp
= NULL
;
10547 bool uj
= use_json(argc
, argv
);
10552 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
10555 return CMD_WARNING
;
10557 argv_find(argv
, argc
, "neighbors", &idx
);
10558 peer
= peer_lookup_in_view(vty
, bgp
, argv
[idx
+ 1]->arg
, uj
);
10560 return CMD_WARNING
;
10562 return bgp_peer_counts(vty
, peer
, AFI_IP
, SAFI_UNICAST
, uj
);
10565 #ifdef KEEP_OLD_VPN_COMMANDS
10566 DEFUN (show_ip_bgp_vpn_neighbor_prefix_counts
,
10567 show_ip_bgp_vpn_neighbor_prefix_counts_cmd
,
10568 "show [ip] bgp <vpnv4|vpnv6> all neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
10573 "Display information about all VPNv4 NLRIs\n"
10574 "Detailed information on TCP and BGP neighbor connections\n"
10575 "Neighbor to display information about\n"
10576 "Neighbor to display information about\n"
10577 "Neighbor on BGP configured interface\n"
10578 "Display detailed prefix count information\n"
10583 bool uj
= use_json(argc
, argv
);
10585 peer
= peer_lookup_in_view(vty
, NULL
, argv
[idx_peer
]->arg
, uj
);
10587 return CMD_WARNING
;
10589 return bgp_peer_counts(vty
, peer
, AFI_IP
, SAFI_MPLS_VPN
, uj
);
10592 DEFUN (show_ip_bgp_vpn_all_route_prefix
,
10593 show_ip_bgp_vpn_all_route_prefix_cmd
,
10594 "show [ip] bgp <vpnv4|vpnv6> all <A.B.C.D|A.B.C.D/M> [json]",
10599 "Display information about all VPNv4 NLRIs\n"
10600 "Network in the BGP routing table to display\n"
10601 "Network in the BGP routing table to display\n"
10605 char *network
= NULL
;
10606 struct bgp
*bgp
= bgp_get_default();
10608 vty_out(vty
, "Can't find default instance\n");
10609 return CMD_WARNING
;
10612 if (argv_find(argv
, argc
, "A.B.C.D", &idx
))
10613 network
= argv
[idx
]->arg
;
10614 else if (argv_find(argv
, argc
, "A.B.C.D/M", &idx
))
10615 network
= argv
[idx
]->arg
;
10617 vty_out(vty
, "Unable to figure out Network\n");
10618 return CMD_WARNING
;
10621 return bgp_show_route(vty
, bgp
, network
, AFI_IP
, SAFI_MPLS_VPN
, NULL
, 0,
10622 BGP_PATH_SHOW_ALL
, use_json(argc
, argv
));
10624 #endif /* KEEP_OLD_VPN_COMMANDS */
10626 DEFUN (show_ip_bgp_l2vpn_evpn_all_route_prefix
,
10627 show_ip_bgp_l2vpn_evpn_all_route_prefix_cmd
,
10628 "show [ip] bgp l2vpn evpn all <A.B.C.D|A.B.C.D/M> [json]",
10634 "Display information about all EVPN NLRIs\n"
10635 "Network in the BGP routing table to display\n"
10636 "Network in the BGP routing table to display\n"
10640 char *network
= NULL
;
10642 if (argv_find(argv
, argc
, "A.B.C.D", &idx
))
10643 network
= argv
[idx
]->arg
;
10644 else if (argv_find(argv
, argc
, "A.B.C.D/M", &idx
))
10645 network
= argv
[idx
]->arg
;
10647 vty_out(vty
, "Unable to figure out Network\n");
10648 return CMD_WARNING
;
10650 return bgp_show_route(vty
, NULL
, network
, AFI_L2VPN
, SAFI_EVPN
, NULL
, 0,
10651 BGP_PATH_SHOW_ALL
, use_json(argc
, argv
));
10654 static void show_adj_route(struct vty
*vty
, struct peer
*peer
, afi_t afi
,
10655 safi_t safi
, enum bgp_show_adj_route_type type
,
10656 const char *rmap_name
, bool use_json
,
10659 struct bgp_table
*table
;
10660 struct bgp_adj_in
*ain
;
10661 struct bgp_adj_out
*adj
;
10662 unsigned long output_count
;
10663 unsigned long filtered_count
;
10664 struct bgp_node
*rn
;
10670 struct update_subgroup
*subgrp
;
10671 json_object
*json_scode
= NULL
;
10672 json_object
*json_ocode
= NULL
;
10673 json_object
*json_ar
= NULL
;
10674 struct peer_af
*paf
;
10675 bool route_filtered
;
10678 json_scode
= json_object_new_object();
10679 json_ocode
= json_object_new_object();
10680 json_ar
= json_object_new_object();
10682 json_object_string_add(json_scode
, "suppressed", "s");
10683 json_object_string_add(json_scode
, "damped", "d");
10684 json_object_string_add(json_scode
, "history", "h");
10685 json_object_string_add(json_scode
, "valid", "*");
10686 json_object_string_add(json_scode
, "best", ">");
10687 json_object_string_add(json_scode
, "multipath", "=");
10688 json_object_string_add(json_scode
, "internal", "i");
10689 json_object_string_add(json_scode
, "ribFailure", "r");
10690 json_object_string_add(json_scode
, "stale", "S");
10691 json_object_string_add(json_scode
, "removed", "R");
10693 json_object_string_add(json_ocode
, "igp", "i");
10694 json_object_string_add(json_ocode
, "egp", "e");
10695 json_object_string_add(json_ocode
, "incomplete", "?");
10702 json_object_string_add(json
, "alert", "no BGP");
10703 vty_out(vty
, "%s\n", json_object_to_json_string(json
));
10704 json_object_free(json
);
10706 vty_out(vty
, "%% No bgp\n");
10710 table
= bgp
->rib
[afi
][safi
];
10712 output_count
= filtered_count
= 0;
10713 subgrp
= peer_subgroup(peer
, afi
, safi
);
10715 if (type
== bgp_show_adj_route_advertised
&& subgrp
10716 && CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
)) {
10718 json_object_int_add(json
, "bgpTableVersion",
10720 json_object_string_add(json
, "bgpLocalRouterId",
10721 inet_ntoa(bgp
->router_id
));
10722 json_object_int_add(json
, "defaultLocPrf",
10723 bgp
->default_local_pref
);
10724 json_object_int_add(json
, "localAS", bgp
->as
);
10725 json_object_object_add(json
, "bgpStatusCodes",
10727 json_object_object_add(json
, "bgpOriginCodes",
10729 json_object_string_add(
10730 json
, "bgpOriginatingDefaultNetwork",
10731 (afi
== AFI_IP
) ? "0.0.0.0/0" : "::/0");
10733 vty_out(vty
, "BGP table version is %" PRIu64
10734 ", local router ID is %s, vrf id ",
10735 table
->version
, inet_ntoa(bgp
->router_id
));
10736 if (bgp
->vrf_id
== VRF_UNKNOWN
)
10737 vty_out(vty
, "%s", VRFID_NONE_STR
);
10739 vty_out(vty
, "%u", bgp
->vrf_id
);
10740 vty_out(vty
, "\n");
10741 vty_out(vty
, "Default local pref %u, ",
10742 bgp
->default_local_pref
);
10743 vty_out(vty
, "local AS %u\n", bgp
->as
);
10744 vty_out(vty
, BGP_SHOW_SCODE_HEADER
);
10745 vty_out(vty
, BGP_SHOW_NCODE_HEADER
);
10746 vty_out(vty
, BGP_SHOW_OCODE_HEADER
);
10748 vty_out(vty
, "Originating default network %s\n\n",
10749 (afi
== AFI_IP
) ? "0.0.0.0/0" : "::/0");
10754 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
10755 if (type
== bgp_show_adj_route_received
10756 || type
== bgp_show_adj_route_filtered
) {
10757 for (ain
= rn
->adj_in
; ain
; ain
= ain
->next
) {
10758 if (ain
->peer
!= peer
|| !ain
->attr
)
10763 json_object_int_add(
10764 json
, "bgpTableVersion",
10766 json_object_string_add(
10768 "bgpLocalRouterId",
10771 json_object_int_add(json
,
10773 bgp
->default_local_pref
);
10774 json_object_int_add(json
,
10775 "localAS", bgp
->as
);
10776 json_object_object_add(
10777 json
, "bgpStatusCodes",
10779 json_object_object_add(
10780 json
, "bgpOriginCodes",
10784 "BGP table version is 0, local router ID is %s, vrf id ",
10787 if (bgp
->vrf_id
== VRF_UNKNOWN
)
10793 vty_out(vty
, "\n");
10795 "Default local pref %u, ",
10796 bgp
->default_local_pref
);
10797 vty_out(vty
, "local AS %u\n",
10800 BGP_SHOW_SCODE_HEADER
);
10802 BGP_SHOW_NCODE_HEADER
);
10804 BGP_SHOW_OCODE_HEADER
);
10810 vty_out(vty
, BGP_SHOW_HEADER
);
10814 bgp_attr_dup(&attr
, ain
->attr
);
10815 route_filtered
= false;
10817 /* Filter prefix using distribute list,
10818 * filter list or prefix list
10820 if ((bgp_input_filter(peer
, &rn
->p
, &attr
, afi
,
10821 safi
)) == FILTER_DENY
)
10822 route_filtered
= true;
10824 /* Filter prefix using route-map */
10825 ret
= bgp_input_modifier(peer
, &rn
->p
, &attr
,
10826 afi
, safi
, rmap_name
);
10828 if (type
== bgp_show_adj_route_filtered
&&
10829 !route_filtered
&& ret
!= RMAP_DENY
) {
10830 bgp_attr_undup(&attr
, ain
->attr
);
10834 if (type
== bgp_show_adj_route_received
&&
10835 (route_filtered
|| ret
== RMAP_DENY
))
10838 route_vty_out_tmp(vty
, &rn
->p
, &attr
, safi
,
10839 use_json
, json_ar
);
10840 bgp_attr_undup(&attr
, ain
->attr
);
10843 } else if (type
== bgp_show_adj_route_advertised
) {
10844 RB_FOREACH (adj
, bgp_adj_out_rb
, &rn
->adj_out
)
10845 SUBGRP_FOREACH_PEER (adj
->subgroup
, paf
) {
10846 if (paf
->peer
!= peer
|| !adj
->attr
)
10851 json_object_int_add(
10855 json_object_string_add(
10857 "bgpLocalRouterId",
10860 json_object_int_add(
10861 json
, "defaultLocPrf",
10862 bgp
->default_local_pref
10864 json_object_int_add(
10867 json_object_object_add(
10871 json_object_object_add(
10877 "BGP table version is %" PRIu64
10878 ", local router ID is %s, vrf id ",
10891 vty_out(vty
, "\n");
10893 "Default local pref %u, ",
10894 bgp
->default_local_pref
10900 BGP_SHOW_SCODE_HEADER
);
10902 BGP_SHOW_NCODE_HEADER
);
10904 BGP_SHOW_OCODE_HEADER
);
10915 bgp_attr_dup(&attr
, adj
->attr
);
10916 ret
= bgp_output_modifier(
10917 peer
, &rn
->p
, &attr
, afi
, safi
,
10920 if (ret
!= RMAP_DENY
) {
10921 route_vty_out_tmp(vty
, &rn
->p
,
10930 bgp_attr_undup(&attr
, adj
->attr
);
10936 json_object_object_add(json
, "advertisedRoutes", json_ar
);
10937 json_object_int_add(json
, "totalPrefixCounter", output_count
);
10938 json_object_int_add(json
, "filteredPrefixCounter",
10941 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
10942 json
, JSON_C_TO_STRING_PRETTY
));
10943 json_object_free(json
);
10944 } else if (output_count
> 0) {
10945 if (filtered_count
> 0)
10947 "\nTotal number of prefixes %ld (%ld filtered)\n",
10948 output_count
, filtered_count
);
10950 vty_out(vty
, "\nTotal number of prefixes %ld\n",
10955 static int peer_adj_routes(struct vty
*vty
, struct peer
*peer
, afi_t afi
,
10956 safi_t safi
, enum bgp_show_adj_route_type type
,
10957 const char *rmap_name
, bool use_json
)
10959 json_object
*json
= NULL
;
10962 json
= json_object_new_object();
10964 /* labeled-unicast routes live in the unicast table */
10965 if (safi
== SAFI_LABELED_UNICAST
)
10966 safi
= SAFI_UNICAST
;
10968 if (!peer
|| !peer
->afc
[afi
][safi
]) {
10970 json_object_string_add(
10972 "No such neighbor or address family");
10973 vty_out(vty
, "%s\n", json_object_to_json_string(json
));
10974 json_object_free(json
);
10976 vty_out(vty
, "%% No such neighbor or address family\n");
10978 return CMD_WARNING
;
10981 if ((type
== bgp_show_adj_route_received
10982 || type
== bgp_show_adj_route_filtered
)
10983 && !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
10984 PEER_FLAG_SOFT_RECONFIG
)) {
10986 json_object_string_add(
10988 "Inbound soft reconfiguration not enabled");
10989 vty_out(vty
, "%s\n", json_object_to_json_string(json
));
10990 json_object_free(json
);
10993 "%% Inbound soft reconfiguration not enabled\n");
10995 return CMD_WARNING
;
10998 show_adj_route(vty
, peer
, afi
, safi
, type
, rmap_name
, use_json
, json
);
11000 return CMD_SUCCESS
;
11003 DEFUN (show_ip_bgp_instance_neighbor_advertised_route
,
11004 show_ip_bgp_instance_neighbor_advertised_route_cmd
,
11005 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] "
11006 "neighbors <A.B.C.D|X:X::X:X|WORD> <advertised-routes|received-routes|filtered-routes> [route-map WORD] [json]",
11010 BGP_INSTANCE_HELP_STR
11012 BGP_SAFI_WITH_LABEL_HELP_STR
11013 "Detailed information on TCP and BGP neighbor connections\n"
11014 "Neighbor to display information about\n"
11015 "Neighbor to display information about\n"
11016 "Neighbor on BGP configured interface\n"
11017 "Display the routes advertised to a BGP neighbor\n"
11018 "Display the received routes from neighbor\n"
11019 "Display the filtered routes received from neighbor\n"
11020 "Route-map to modify the attributes\n"
11021 "Name of the route map\n"
11024 afi_t afi
= AFI_IP6
;
11025 safi_t safi
= SAFI_UNICAST
;
11026 char *rmap_name
= NULL
;
11027 char *peerstr
= NULL
;
11028 struct bgp
*bgp
= NULL
;
11030 enum bgp_show_adj_route_type type
= bgp_show_adj_route_advertised
;
11032 bool uj
= use_json(argc
, argv
);
11037 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
11040 return CMD_WARNING
;
11042 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
11043 argv_find(argv
, argc
, "neighbors", &idx
);
11044 peerstr
= argv
[++idx
]->arg
;
11046 peer
= peer_lookup_in_view(vty
, bgp
, peerstr
, uj
);
11048 return CMD_WARNING
;
11050 if (argv_find(argv
, argc
, "advertised-routes", &idx
))
11051 type
= bgp_show_adj_route_advertised
;
11052 else if (argv_find(argv
, argc
, "received-routes", &idx
))
11053 type
= bgp_show_adj_route_received
;
11054 else if (argv_find(argv
, argc
, "filtered-routes", &idx
))
11055 type
= bgp_show_adj_route_filtered
;
11057 if (argv_find(argv
, argc
, "route-map", &idx
))
11058 rmap_name
= argv
[++idx
]->arg
;
11060 return peer_adj_routes(vty
, peer
, afi
, safi
, type
, rmap_name
, uj
);
11063 DEFUN (show_ip_bgp_neighbor_received_prefix_filter
,
11064 show_ip_bgp_neighbor_received_prefix_filter_cmd
,
11065 "show [ip] bgp [<ipv4|ipv6> [unicast]] neighbors <A.B.C.D|X:X::X:X|WORD> received prefix-filter [json]",
11071 "Address Family modifier\n"
11072 "Detailed information on TCP and BGP neighbor connections\n"
11073 "Neighbor to display information about\n"
11074 "Neighbor to display information about\n"
11075 "Neighbor on BGP configured interface\n"
11076 "Display information received from a BGP neighbor\n"
11077 "Display the prefixlist filter\n"
11080 afi_t afi
= AFI_IP6
;
11081 safi_t safi
= SAFI_UNICAST
;
11082 char *peerstr
= NULL
;
11085 union sockunion su
;
11091 /* show [ip] bgp */
11092 if (argv_find(argv
, argc
, "ip", &idx
))
11094 /* [<ipv4|ipv6> [unicast]] */
11095 if (argv_find(argv
, argc
, "ipv4", &idx
))
11097 if (argv_find(argv
, argc
, "ipv6", &idx
))
11099 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
11100 argv_find(argv
, argc
, "neighbors", &idx
);
11101 peerstr
= argv
[++idx
]->arg
;
11103 bool uj
= use_json(argc
, argv
);
11105 ret
= str2sockunion(peerstr
, &su
);
11107 peer
= peer_lookup_by_conf_if(NULL
, peerstr
);
11110 vty_out(vty
, "{}\n");
11113 "%% Malformed address or name: %s\n",
11115 return CMD_WARNING
;
11118 peer
= peer_lookup(NULL
, &su
);
11121 vty_out(vty
, "{}\n");
11123 vty_out(vty
, "No peer\n");
11124 return CMD_WARNING
;
11128 sprintf(name
, "%s.%d.%d", peer
->host
, afi
, safi
);
11129 count
= prefix_bgp_show_prefix_list(NULL
, afi
, name
, uj
);
11132 vty_out(vty
, "Address Family: %s\n",
11133 afi_safi_print(afi
, safi
));
11134 prefix_bgp_show_prefix_list(vty
, afi
, name
, uj
);
11137 vty_out(vty
, "{}\n");
11139 vty_out(vty
, "No functional output\n");
11142 return CMD_SUCCESS
;
11145 static int bgp_show_neighbor_route(struct vty
*vty
, struct peer
*peer
,
11146 afi_t afi
, safi_t safi
,
11147 enum bgp_show_type type
, bool use_json
)
11149 /* labeled-unicast routes live in the unicast table */
11150 if (safi
== SAFI_LABELED_UNICAST
)
11151 safi
= SAFI_UNICAST
;
11153 if (!peer
|| !peer
->afc
[afi
][safi
]) {
11155 json_object
*json_no
= NULL
;
11156 json_no
= json_object_new_object();
11157 json_object_string_add(
11158 json_no
, "warning",
11159 "No such neighbor or address family");
11160 vty_out(vty
, "%s\n",
11161 json_object_to_json_string(json_no
));
11162 json_object_free(json_no
);
11164 vty_out(vty
, "%% No such neighbor or address family\n");
11165 return CMD_WARNING
;
11168 return bgp_show(vty
, peer
->bgp
, afi
, safi
, type
, &peer
->su
, use_json
);
11171 DEFUN (show_ip_bgp_flowspec_routes_detailed
,
11172 show_ip_bgp_flowspec_routes_detailed_cmd
,
11173 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" flowspec] detail [json]",
11177 BGP_INSTANCE_HELP_STR
11180 "Detailed information on flowspec entries\n"
11183 afi_t afi
= AFI_IP
;
11184 safi_t safi
= SAFI_UNICAST
;
11185 struct bgp
*bgp
= NULL
;
11187 bool uj
= use_json(argc
, argv
);
11192 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
11195 return CMD_WARNING
;
11197 return bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_detail
, NULL
, uj
);
11200 DEFUN (show_ip_bgp_neighbor_routes
,
11201 show_ip_bgp_neighbor_routes_cmd
,
11202 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] "
11203 "neighbors <A.B.C.D|X:X::X:X|WORD> <flap-statistics|dampened-routes|routes> [json]",
11207 BGP_INSTANCE_HELP_STR
11209 BGP_SAFI_WITH_LABEL_HELP_STR
11210 "Detailed information on TCP and BGP neighbor connections\n"
11211 "Neighbor to display information about\n"
11212 "Neighbor to display information about\n"
11213 "Neighbor on BGP configured interface\n"
11214 "Display flap statistics of the routes learned from neighbor\n"
11215 "Display the dampened routes received from neighbor\n"
11216 "Display routes learned from neighbor\n"
11219 char *peerstr
= NULL
;
11220 struct bgp
*bgp
= NULL
;
11221 afi_t afi
= AFI_IP6
;
11222 safi_t safi
= SAFI_UNICAST
;
11224 enum bgp_show_type sh_type
= bgp_show_type_neighbor
;
11226 bool uj
= use_json(argc
, argv
);
11231 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
11234 return CMD_WARNING
;
11236 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
11237 argv_find(argv
, argc
, "neighbors", &idx
);
11238 peerstr
= argv
[++idx
]->arg
;
11240 peer
= peer_lookup_in_view(vty
, bgp
, peerstr
, uj
);
11242 return CMD_WARNING
;
11244 if (argv_find(argv
, argc
, "flap-statistics", &idx
))
11245 sh_type
= bgp_show_type_flap_neighbor
;
11246 else if (argv_find(argv
, argc
, "dampened-routes", &idx
))
11247 sh_type
= bgp_show_type_damp_neighbor
;
11248 else if (argv_find(argv
, argc
, "routes", &idx
))
11249 sh_type
= bgp_show_type_neighbor
;
11251 return bgp_show_neighbor_route(vty
, peer
, afi
, safi
, sh_type
, uj
);
11254 struct bgp_table
*bgp_distance_table
[AFI_MAX
][SAFI_MAX
];
11256 struct bgp_distance
{
11257 /* Distance value for the IP source prefix. */
11260 /* Name of the access-list to be matched. */
11264 DEFUN (show_bgp_afi_vpn_rd_route
,
11265 show_bgp_afi_vpn_rd_route_cmd
,
11266 "show bgp "BGP_AFI_CMD_STR
" vpn rd ASN:NN_OR_IP-ADDRESS:NN <A.B.C.D/M|X:X::X:X/M> [json]",
11270 "Address Family modifier\n"
11271 "Display information for a route distinguisher\n"
11272 "Route Distinguisher\n"
11273 "Network in the BGP routing table to display\n"
11274 "Network in the BGP routing table to display\n"
11278 struct prefix_rd prd
;
11279 afi_t afi
= AFI_MAX
;
11282 if (!argv_find_and_parse_afi(argv
, argc
, &idx
, &afi
)) {
11283 vty_out(vty
, "%% Malformed Address Family\n");
11284 return CMD_WARNING
;
11287 ret
= str2prefix_rd(argv
[5]->arg
, &prd
);
11289 vty_out(vty
, "%% Malformed Route Distinguisher\n");
11290 return CMD_WARNING
;
11293 return bgp_show_route(vty
, NULL
, argv
[6]->arg
, afi
, SAFI_MPLS_VPN
, &prd
,
11294 0, BGP_PATH_SHOW_ALL
, use_json(argc
, argv
));
11297 static struct bgp_distance
*bgp_distance_new(void)
11299 return XCALLOC(MTYPE_BGP_DISTANCE
, sizeof(struct bgp_distance
));
11302 static void bgp_distance_free(struct bgp_distance
*bdistance
)
11304 XFREE(MTYPE_BGP_DISTANCE
, bdistance
);
11307 static int bgp_distance_set(struct vty
*vty
, const char *distance_str
,
11308 const char *ip_str
, const char *access_list_str
)
11315 struct bgp_node
*rn
;
11316 struct bgp_distance
*bdistance
;
11318 afi
= bgp_node_afi(vty
);
11319 safi
= bgp_node_safi(vty
);
11321 ret
= str2prefix(ip_str
, &p
);
11323 vty_out(vty
, "Malformed prefix\n");
11324 return CMD_WARNING_CONFIG_FAILED
;
11327 distance
= atoi(distance_str
);
11329 /* Get BGP distance node. */
11330 rn
= bgp_node_get(bgp_distance_table
[afi
][safi
], (struct prefix
*)&p
);
11331 bdistance
= bgp_node_get_bgp_distance_info(rn
);
11333 bgp_unlock_node(rn
);
11335 bdistance
= bgp_distance_new();
11336 bgp_node_set_bgp_distance_info(rn
, bdistance
);
11339 /* Set distance value. */
11340 bdistance
->distance
= distance
;
11342 /* Reset access-list configuration. */
11343 if (bdistance
->access_list
) {
11344 XFREE(MTYPE_AS_LIST
, bdistance
->access_list
);
11345 bdistance
->access_list
= NULL
;
11347 if (access_list_str
)
11348 bdistance
->access_list
=
11349 XSTRDUP(MTYPE_AS_LIST
, access_list_str
);
11351 return CMD_SUCCESS
;
11354 static int bgp_distance_unset(struct vty
*vty
, const char *distance_str
,
11355 const char *ip_str
, const char *access_list_str
)
11362 struct bgp_node
*rn
;
11363 struct bgp_distance
*bdistance
;
11365 afi
= bgp_node_afi(vty
);
11366 safi
= bgp_node_safi(vty
);
11368 ret
= str2prefix(ip_str
, &p
);
11370 vty_out(vty
, "Malformed prefix\n");
11371 return CMD_WARNING_CONFIG_FAILED
;
11374 rn
= bgp_node_lookup(bgp_distance_table
[afi
][safi
],
11375 (struct prefix
*)&p
);
11377 vty_out(vty
, "Can't find specified prefix\n");
11378 return CMD_WARNING_CONFIG_FAILED
;
11381 bdistance
= bgp_node_get_bgp_distance_info(rn
);
11382 distance
= atoi(distance_str
);
11384 if (bdistance
->distance
!= distance
) {
11385 vty_out(vty
, "Distance does not match configured\n");
11386 return CMD_WARNING_CONFIG_FAILED
;
11389 if (bdistance
->access_list
)
11390 XFREE(MTYPE_AS_LIST
, bdistance
->access_list
);
11391 bgp_distance_free(bdistance
);
11393 bgp_node_set_bgp_path_info(rn
, NULL
);
11394 bgp_unlock_node(rn
);
11395 bgp_unlock_node(rn
);
11397 return CMD_SUCCESS
;
11400 /* Apply BGP information to distance method. */
11401 uint8_t bgp_distance_apply(struct prefix
*p
, struct bgp_path_info
*pinfo
,
11402 afi_t afi
, safi_t safi
, struct bgp
*bgp
)
11404 struct bgp_node
*rn
;
11407 struct bgp_distance
*bdistance
;
11408 struct access_list
*alist
;
11409 struct bgp_static
*bgp_static
;
11414 peer
= pinfo
->peer
;
11416 /* Check source address. */
11417 sockunion2hostprefix(&peer
->su
, &q
);
11418 rn
= bgp_node_match(bgp_distance_table
[afi
][safi
], &q
);
11420 bdistance
= bgp_node_get_bgp_distance_info(rn
);
11421 bgp_unlock_node(rn
);
11423 if (bdistance
->access_list
) {
11424 alist
= access_list_lookup(afi
, bdistance
->access_list
);
11426 && access_list_apply(alist
, p
) == FILTER_PERMIT
)
11427 return bdistance
->distance
;
11429 return bdistance
->distance
;
11432 /* Backdoor check. */
11433 rn
= bgp_node_lookup(bgp
->route
[afi
][safi
], p
);
11435 bgp_static
= bgp_node_get_bgp_static_info(rn
);
11436 bgp_unlock_node(rn
);
11438 if (bgp_static
->backdoor
) {
11439 if (bgp
->distance_local
[afi
][safi
])
11440 return bgp
->distance_local
[afi
][safi
];
11442 return ZEBRA_IBGP_DISTANCE_DEFAULT
;
11446 if (peer
->sort
== BGP_PEER_EBGP
) {
11447 if (bgp
->distance_ebgp
[afi
][safi
])
11448 return bgp
->distance_ebgp
[afi
][safi
];
11449 return ZEBRA_EBGP_DISTANCE_DEFAULT
;
11451 if (bgp
->distance_ibgp
[afi
][safi
])
11452 return bgp
->distance_ibgp
[afi
][safi
];
11453 return ZEBRA_IBGP_DISTANCE_DEFAULT
;
11457 DEFUN (bgp_distance
,
11459 "distance bgp (1-255) (1-255) (1-255)",
11460 "Define an administrative distance\n"
11462 "Distance for routes external to the AS\n"
11463 "Distance for routes internal to the AS\n"
11464 "Distance for local routes\n")
11466 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
11467 int idx_number
= 2;
11468 int idx_number_2
= 3;
11469 int idx_number_3
= 4;
11473 afi
= bgp_node_afi(vty
);
11474 safi
= bgp_node_safi(vty
);
11476 bgp
->distance_ebgp
[afi
][safi
] = atoi(argv
[idx_number
]->arg
);
11477 bgp
->distance_ibgp
[afi
][safi
] = atoi(argv
[idx_number_2
]->arg
);
11478 bgp
->distance_local
[afi
][safi
] = atoi(argv
[idx_number_3
]->arg
);
11479 return CMD_SUCCESS
;
11482 DEFUN (no_bgp_distance
,
11483 no_bgp_distance_cmd
,
11484 "no distance bgp [(1-255) (1-255) (1-255)]",
11486 "Define an administrative distance\n"
11488 "Distance for routes external to the AS\n"
11489 "Distance for routes internal to the AS\n"
11490 "Distance for local routes\n")
11492 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
11496 afi
= bgp_node_afi(vty
);
11497 safi
= bgp_node_safi(vty
);
11499 bgp
->distance_ebgp
[afi
][safi
] = 0;
11500 bgp
->distance_ibgp
[afi
][safi
] = 0;
11501 bgp
->distance_local
[afi
][safi
] = 0;
11502 return CMD_SUCCESS
;
11506 DEFUN (bgp_distance_source
,
11507 bgp_distance_source_cmd
,
11508 "distance (1-255) A.B.C.D/M",
11509 "Define an administrative distance\n"
11510 "Administrative distance\n"
11511 "IP source prefix\n")
11513 int idx_number
= 1;
11514 int idx_ipv4_prefixlen
= 2;
11515 bgp_distance_set(vty
, argv
[idx_number
]->arg
,
11516 argv
[idx_ipv4_prefixlen
]->arg
, NULL
);
11517 return CMD_SUCCESS
;
11520 DEFUN (no_bgp_distance_source
,
11521 no_bgp_distance_source_cmd
,
11522 "no distance (1-255) A.B.C.D/M",
11524 "Define an administrative distance\n"
11525 "Administrative distance\n"
11526 "IP source prefix\n")
11528 int idx_number
= 2;
11529 int idx_ipv4_prefixlen
= 3;
11530 bgp_distance_unset(vty
, argv
[idx_number
]->arg
,
11531 argv
[idx_ipv4_prefixlen
]->arg
, NULL
);
11532 return CMD_SUCCESS
;
11535 DEFUN (bgp_distance_source_access_list
,
11536 bgp_distance_source_access_list_cmd
,
11537 "distance (1-255) A.B.C.D/M WORD",
11538 "Define an administrative distance\n"
11539 "Administrative distance\n"
11540 "IP source prefix\n"
11541 "Access list name\n")
11543 int idx_number
= 1;
11544 int idx_ipv4_prefixlen
= 2;
11546 bgp_distance_set(vty
, argv
[idx_number
]->arg
,
11547 argv
[idx_ipv4_prefixlen
]->arg
, argv
[idx_word
]->arg
);
11548 return CMD_SUCCESS
;
11551 DEFUN (no_bgp_distance_source_access_list
,
11552 no_bgp_distance_source_access_list_cmd
,
11553 "no distance (1-255) A.B.C.D/M WORD",
11555 "Define an administrative distance\n"
11556 "Administrative distance\n"
11557 "IP source prefix\n"
11558 "Access list name\n")
11560 int idx_number
= 2;
11561 int idx_ipv4_prefixlen
= 3;
11563 bgp_distance_unset(vty
, argv
[idx_number
]->arg
,
11564 argv
[idx_ipv4_prefixlen
]->arg
, argv
[idx_word
]->arg
);
11565 return CMD_SUCCESS
;
11568 DEFUN (ipv6_bgp_distance_source
,
11569 ipv6_bgp_distance_source_cmd
,
11570 "distance (1-255) X:X::X:X/M",
11571 "Define an administrative distance\n"
11572 "Administrative distance\n"
11573 "IP source prefix\n")
11575 bgp_distance_set(vty
, argv
[1]->arg
, argv
[2]->arg
, NULL
);
11576 return CMD_SUCCESS
;
11579 DEFUN (no_ipv6_bgp_distance_source
,
11580 no_ipv6_bgp_distance_source_cmd
,
11581 "no distance (1-255) X:X::X:X/M",
11583 "Define an administrative distance\n"
11584 "Administrative distance\n"
11585 "IP source prefix\n")
11587 bgp_distance_unset(vty
, argv
[2]->arg
, argv
[3]->arg
, NULL
);
11588 return CMD_SUCCESS
;
11591 DEFUN (ipv6_bgp_distance_source_access_list
,
11592 ipv6_bgp_distance_source_access_list_cmd
,
11593 "distance (1-255) X:X::X:X/M WORD",
11594 "Define an administrative distance\n"
11595 "Administrative distance\n"
11596 "IP source prefix\n"
11597 "Access list name\n")
11599 bgp_distance_set(vty
, argv
[1]->arg
, argv
[2]->arg
, argv
[3]->arg
);
11600 return CMD_SUCCESS
;
11603 DEFUN (no_ipv6_bgp_distance_source_access_list
,
11604 no_ipv6_bgp_distance_source_access_list_cmd
,
11605 "no distance (1-255) X:X::X:X/M WORD",
11607 "Define an administrative distance\n"
11608 "Administrative distance\n"
11609 "IP source prefix\n"
11610 "Access list name\n")
11612 bgp_distance_unset(vty
, argv
[2]->arg
, argv
[3]->arg
, argv
[4]->arg
);
11613 return CMD_SUCCESS
;
11616 DEFUN (bgp_damp_set
,
11618 "bgp dampening [(1-45) [(1-20000) (1-20000) (1-255)]]",
11619 "BGP Specific commands\n"
11620 "Enable route-flap dampening\n"
11621 "Half-life time for the penalty\n"
11622 "Value to start reusing a route\n"
11623 "Value to start suppressing a route\n"
11624 "Maximum duration to suppress a stable route\n")
11626 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
11627 int idx_half_life
= 2;
11629 int idx_suppress
= 4;
11630 int idx_max_suppress
= 5;
11631 int half
= DEFAULT_HALF_LIFE
* 60;
11632 int reuse
= DEFAULT_REUSE
;
11633 int suppress
= DEFAULT_SUPPRESS
;
11634 int max
= 4 * half
;
11637 half
= atoi(argv
[idx_half_life
]->arg
) * 60;
11638 reuse
= atoi(argv
[idx_reuse
]->arg
);
11639 suppress
= atoi(argv
[idx_suppress
]->arg
);
11640 max
= atoi(argv
[idx_max_suppress
]->arg
) * 60;
11641 } else if (argc
== 3) {
11642 half
= atoi(argv
[idx_half_life
]->arg
) * 60;
11646 if (suppress
< reuse
) {
11648 "Suppress value cannot be less than reuse value \n");
11652 return bgp_damp_enable(bgp
, bgp_node_afi(vty
), bgp_node_safi(vty
), half
,
11653 reuse
, suppress
, max
);
11656 DEFUN (bgp_damp_unset
,
11657 bgp_damp_unset_cmd
,
11658 "no bgp dampening [(1-45) [(1-20000) (1-20000) (1-255)]]",
11660 "BGP Specific commands\n"
11661 "Enable route-flap dampening\n"
11662 "Half-life time for the penalty\n"
11663 "Value to start reusing a route\n"
11664 "Value to start suppressing a route\n"
11665 "Maximum duration to suppress a stable route\n")
11667 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
11668 return bgp_damp_disable(bgp
, bgp_node_afi(vty
), bgp_node_safi(vty
));
11671 /* Display specified route of BGP table. */
11672 static int bgp_clear_damp_route(struct vty
*vty
, const char *view_name
,
11673 const char *ip_str
, afi_t afi
, safi_t safi
,
11674 struct prefix_rd
*prd
, int prefix_check
)
11677 struct prefix match
;
11678 struct bgp_node
*rn
;
11679 struct bgp_node
*rm
;
11680 struct bgp_path_info
*pi
;
11681 struct bgp_path_info
*pi_temp
;
11683 struct bgp_table
*table
;
11685 /* BGP structure lookup. */
11687 bgp
= bgp_lookup_by_name(view_name
);
11689 vty_out(vty
, "%% Can't find BGP instance %s\n",
11691 return CMD_WARNING
;
11694 bgp
= bgp_get_default();
11696 vty_out(vty
, "%% No BGP process is configured\n");
11697 return CMD_WARNING
;
11701 /* Check IP address argument. */
11702 ret
= str2prefix(ip_str
, &match
);
11704 vty_out(vty
, "%% address is malformed\n");
11705 return CMD_WARNING
;
11708 match
.family
= afi2family(afi
);
11710 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
11711 || (safi
== SAFI_EVPN
)) {
11712 for (rn
= bgp_table_top(bgp
->rib
[AFI_IP
][safi
]); rn
;
11713 rn
= bgp_route_next(rn
)) {
11714 if (prd
&& memcmp(rn
->p
.u
.val
, prd
->val
, 8) != 0)
11716 table
= bgp_node_get_bgp_table_info(rn
);
11719 if ((rm
= bgp_node_match(table
, &match
)) == NULL
)
11723 || rm
->p
.prefixlen
== match
.prefixlen
) {
11724 pi
= bgp_node_get_bgp_path_info(rm
);
11726 if (pi
->extra
&& pi
->extra
->damp_info
) {
11727 pi_temp
= pi
->next
;
11728 bgp_damp_info_free(
11729 pi
->extra
->damp_info
,
11737 bgp_unlock_node(rm
);
11740 if ((rn
= bgp_node_match(bgp
->rib
[afi
][safi
], &match
))
11743 || rn
->p
.prefixlen
== match
.prefixlen
) {
11744 pi
= bgp_node_get_bgp_path_info(rn
);
11746 if (pi
->extra
&& pi
->extra
->damp_info
) {
11747 pi_temp
= pi
->next
;
11748 bgp_damp_info_free(
11749 pi
->extra
->damp_info
,
11757 bgp_unlock_node(rn
);
11761 return CMD_SUCCESS
;
11764 DEFUN (clear_ip_bgp_dampening
,
11765 clear_ip_bgp_dampening_cmd
,
11766 "clear ip bgp dampening",
11770 "Clear route flap dampening information\n")
11772 bgp_damp_info_clean();
11773 return CMD_SUCCESS
;
11776 DEFUN (clear_ip_bgp_dampening_prefix
,
11777 clear_ip_bgp_dampening_prefix_cmd
,
11778 "clear ip bgp dampening A.B.C.D/M",
11782 "Clear route flap dampening information\n"
11785 int idx_ipv4_prefixlen
= 4;
11786 return bgp_clear_damp_route(vty
, NULL
, argv
[idx_ipv4_prefixlen
]->arg
,
11787 AFI_IP
, SAFI_UNICAST
, NULL
, 1);
11790 DEFUN (clear_ip_bgp_dampening_address
,
11791 clear_ip_bgp_dampening_address_cmd
,
11792 "clear ip bgp dampening A.B.C.D",
11796 "Clear route flap dampening information\n"
11797 "Network to clear damping information\n")
11800 return bgp_clear_damp_route(vty
, NULL
, argv
[idx_ipv4
]->arg
, AFI_IP
,
11801 SAFI_UNICAST
, NULL
, 0);
11804 DEFUN (clear_ip_bgp_dampening_address_mask
,
11805 clear_ip_bgp_dampening_address_mask_cmd
,
11806 "clear ip bgp dampening A.B.C.D A.B.C.D",
11810 "Clear route flap dampening information\n"
11811 "Network to clear damping information\n"
11815 int idx_ipv4_2
= 5;
11817 char prefix_str
[BUFSIZ
];
11819 ret
= netmask_str2prefix_str(argv
[idx_ipv4
]->arg
, argv
[idx_ipv4_2
]->arg
,
11822 vty_out(vty
, "%% Inconsistent address and mask\n");
11823 return CMD_WARNING
;
11826 return bgp_clear_damp_route(vty
, NULL
, prefix_str
, AFI_IP
, SAFI_UNICAST
,
11830 static void show_bgp_peerhash_entry(struct hash_backet
*backet
, void *arg
)
11832 struct vty
*vty
= arg
;
11833 struct peer
*peer
= backet
->data
;
11834 char buf
[SU_ADDRSTRLEN
];
11836 vty_out(vty
, "\tPeer: %s %s\n", peer
->host
,
11837 sockunion2str(&peer
->su
, buf
, sizeof(buf
)));
11840 DEFUN (show_bgp_peerhash
,
11841 show_bgp_peerhash_cmd
,
11842 "show bgp peerhash",
11845 "Display information about the BGP peerhash\n")
11847 struct list
*instances
= bm
->bgp
;
11848 struct listnode
*node
;
11851 for (ALL_LIST_ELEMENTS_RO(instances
, node
, bgp
)) {
11852 vty_out(vty
, "BGP: %s\n", bgp
->name
);
11853 hash_iterate(bgp
->peerhash
, show_bgp_peerhash_entry
,
11857 return CMD_SUCCESS
;
11860 /* also used for encap safi */
11861 static void bgp_config_write_network_vpn(struct vty
*vty
, struct bgp
*bgp
,
11862 afi_t afi
, safi_t safi
)
11864 struct bgp_node
*prn
;
11865 struct bgp_node
*rn
;
11866 struct bgp_table
*table
;
11868 struct prefix_rd
*prd
;
11869 struct bgp_static
*bgp_static
;
11870 mpls_label_t label
;
11871 char buf
[SU_ADDRSTRLEN
];
11872 char rdbuf
[RD_ADDRSTRLEN
];
11874 /* Network configuration. */
11875 for (prn
= bgp_table_top(bgp
->route
[afi
][safi
]); prn
;
11876 prn
= bgp_route_next(prn
)) {
11877 table
= bgp_node_get_bgp_table_info(prn
);
11881 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
11882 bgp_static
= bgp_node_get_bgp_static_info(rn
);
11883 if (bgp_static
== NULL
)
11887 prd
= (struct prefix_rd
*)&prn
->p
;
11889 /* "network" configuration display. */
11890 prefix_rd2str(prd
, rdbuf
, sizeof(rdbuf
));
11891 label
= decode_label(&bgp_static
->label
);
11893 vty_out(vty
, " network %s/%d rd %s",
11894 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
11896 p
->prefixlen
, rdbuf
);
11897 if (safi
== SAFI_MPLS_VPN
)
11898 vty_out(vty
, " label %u", label
);
11900 if (bgp_static
->rmap
.name
)
11901 vty_out(vty
, " route-map %s",
11902 bgp_static
->rmap
.name
);
11904 if (bgp_static
->backdoor
)
11905 vty_out(vty
, " backdoor");
11907 vty_out(vty
, "\n");
11912 static void bgp_config_write_network_evpn(struct vty
*vty
, struct bgp
*bgp
,
11913 afi_t afi
, safi_t safi
)
11915 struct bgp_node
*prn
;
11916 struct bgp_node
*rn
;
11917 struct bgp_table
*table
;
11919 struct prefix_rd
*prd
;
11920 struct bgp_static
*bgp_static
;
11921 char buf
[PREFIX_STRLEN
* 2];
11922 char buf2
[SU_ADDRSTRLEN
];
11923 char rdbuf
[RD_ADDRSTRLEN
];
11925 /* Network configuration. */
11926 for (prn
= bgp_table_top(bgp
->route
[afi
][safi
]); prn
;
11927 prn
= bgp_route_next(prn
)) {
11928 table
= bgp_node_get_bgp_table_info(prn
);
11932 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
11933 bgp_static
= bgp_node_get_bgp_static_info(rn
);
11934 if (bgp_static
== NULL
)
11937 char *macrouter
= NULL
;
11940 if (bgp_static
->router_mac
)
11941 macrouter
= prefix_mac2str(
11942 bgp_static
->router_mac
, NULL
, 0);
11943 if (bgp_static
->eth_s_id
)
11944 esi
= esi2str(bgp_static
->eth_s_id
);
11946 prd
= (struct prefix_rd
*)&prn
->p
;
11948 /* "network" configuration display. */
11949 prefix_rd2str(prd
, rdbuf
, sizeof(rdbuf
));
11950 if (p
->u
.prefix_evpn
.route_type
== 5) {
11951 char local_buf
[PREFIX_STRLEN
];
11952 uint8_t family
= is_evpn_prefix_ipaddr_v4((
11953 struct prefix_evpn
*)p
)
11957 &p
->u
.prefix_evpn
.prefix_addr
.ip
.ip
.addr
,
11958 local_buf
, PREFIX_STRLEN
);
11959 sprintf(buf
, "%s/%u", local_buf
,
11960 p
->u
.prefix_evpn
.prefix_addr
.ip_prefix_length
);
11962 prefix2str(p
, buf
, sizeof(buf
));
11965 if (bgp_static
->gatewayIp
.family
== AF_INET
11966 || bgp_static
->gatewayIp
.family
== AF_INET6
)
11967 inet_ntop(bgp_static
->gatewayIp
.family
,
11968 &bgp_static
->gatewayIp
.u
.prefix
, buf2
,
11971 " network %s rd %s ethtag %u label %u esi %s gwip %s routermac %s\n",
11973 p
->u
.prefix_evpn
.prefix_addr
.eth_tag
,
11974 decode_label(&bgp_static
->label
), esi
, buf2
,
11978 XFREE(MTYPE_TMP
, macrouter
);
11980 XFREE(MTYPE_TMP
, esi
);
11985 /* Configuration of static route announcement and aggregate
11987 void bgp_config_write_network(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
11990 struct bgp_node
*rn
;
11992 struct bgp_static
*bgp_static
;
11993 struct bgp_aggregate
*bgp_aggregate
;
11994 char buf
[SU_ADDRSTRLEN
];
11996 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)) {
11997 bgp_config_write_network_vpn(vty
, bgp
, afi
, safi
);
12001 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
12002 bgp_config_write_network_evpn(vty
, bgp
, afi
, safi
);
12006 /* Network configuration. */
12007 for (rn
= bgp_table_top(bgp
->route
[afi
][safi
]); rn
;
12008 rn
= bgp_route_next(rn
)) {
12009 bgp_static
= bgp_node_get_bgp_static_info(rn
);
12010 if (bgp_static
== NULL
)
12015 /* "network" configuration display. */
12016 if (bgp_option_check(BGP_OPT_CONFIG_CISCO
) && afi
== AFI_IP
) {
12017 uint32_t destination
;
12018 struct in_addr netmask
;
12020 destination
= ntohl(p
->u
.prefix4
.s_addr
);
12021 masklen2ip(p
->prefixlen
, &netmask
);
12022 vty_out(vty
, " network %s",
12023 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
12026 if ((IN_CLASSC(destination
) && p
->prefixlen
== 24)
12027 || (IN_CLASSB(destination
) && p
->prefixlen
== 16)
12028 || (IN_CLASSA(destination
) && p
->prefixlen
== 8)
12029 || p
->u
.prefix4
.s_addr
== 0) {
12030 /* Natural mask is not display. */
12032 vty_out(vty
, " mask %s", inet_ntoa(netmask
));
12034 vty_out(vty
, " network %s/%d",
12035 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
12040 if (bgp_static
->label_index
!= BGP_INVALID_LABEL_INDEX
)
12041 vty_out(vty
, " label-index %u",
12042 bgp_static
->label_index
);
12044 if (bgp_static
->rmap
.name
)
12045 vty_out(vty
, " route-map %s", bgp_static
->rmap
.name
);
12047 if (bgp_static
->backdoor
)
12048 vty_out(vty
, " backdoor");
12050 vty_out(vty
, "\n");
12053 /* Aggregate-address configuration. */
12054 for (rn
= bgp_table_top(bgp
->aggregate
[afi
][safi
]); rn
;
12055 rn
= bgp_route_next(rn
)) {
12056 bgp_aggregate
= bgp_node_get_bgp_aggregate_info(rn
);
12057 if (bgp_aggregate
== NULL
)
12062 if (bgp_option_check(BGP_OPT_CONFIG_CISCO
) && afi
== AFI_IP
) {
12063 struct in_addr netmask
;
12065 masklen2ip(p
->prefixlen
, &netmask
);
12066 vty_out(vty
, " aggregate-address %s %s",
12067 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
12069 inet_ntoa(netmask
));
12071 vty_out(vty
, " aggregate-address %s/%d",
12072 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
12077 if (bgp_aggregate
->as_set
)
12078 vty_out(vty
, " as-set");
12080 if (bgp_aggregate
->summary_only
)
12081 vty_out(vty
, " summary-only");
12083 vty_out(vty
, "\n");
12087 void bgp_config_write_distance(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
12090 struct bgp_node
*rn
;
12091 struct bgp_distance
*bdistance
;
12093 /* Distance configuration. */
12094 if (bgp
->distance_ebgp
[afi
][safi
] && bgp
->distance_ibgp
[afi
][safi
]
12095 && bgp
->distance_local
[afi
][safi
]
12096 && (bgp
->distance_ebgp
[afi
][safi
] != ZEBRA_EBGP_DISTANCE_DEFAULT
12097 || bgp
->distance_ibgp
[afi
][safi
] != ZEBRA_IBGP_DISTANCE_DEFAULT
12098 || bgp
->distance_local
[afi
][safi
]
12099 != ZEBRA_IBGP_DISTANCE_DEFAULT
)) {
12100 vty_out(vty
, " distance bgp %d %d %d\n",
12101 bgp
->distance_ebgp
[afi
][safi
],
12102 bgp
->distance_ibgp
[afi
][safi
],
12103 bgp
->distance_local
[afi
][safi
]);
12106 for (rn
= bgp_table_top(bgp_distance_table
[afi
][safi
]); rn
;
12107 rn
= bgp_route_next(rn
)) {
12108 bdistance
= bgp_node_get_bgp_distance_info(rn
);
12109 if (bdistance
!= NULL
) {
12110 char buf
[PREFIX_STRLEN
];
12112 vty_out(vty
, " distance %d %s %s\n",
12113 bdistance
->distance
,
12114 prefix2str(&rn
->p
, buf
, sizeof(buf
)),
12115 bdistance
->access_list
? bdistance
->access_list
12121 /* Allocate routing table structure and install commands. */
12122 void bgp_route_init(void)
12127 /* Init BGP distance table. */
12128 FOREACH_AFI_SAFI (afi
, safi
)
12129 bgp_distance_table
[afi
][safi
] = bgp_table_init(NULL
, afi
, safi
);
12131 /* IPv4 BGP commands. */
12132 install_element(BGP_NODE
, &bgp_table_map_cmd
);
12133 install_element(BGP_NODE
, &bgp_network_cmd
);
12134 install_element(BGP_NODE
, &no_bgp_table_map_cmd
);
12136 install_element(BGP_NODE
, &aggregate_address_cmd
);
12137 install_element(BGP_NODE
, &aggregate_address_mask_cmd
);
12138 install_element(BGP_NODE
, &no_aggregate_address_cmd
);
12139 install_element(BGP_NODE
, &no_aggregate_address_mask_cmd
);
12141 /* IPv4 unicast configuration. */
12142 install_element(BGP_IPV4_NODE
, &bgp_table_map_cmd
);
12143 install_element(BGP_IPV4_NODE
, &bgp_network_cmd
);
12144 install_element(BGP_IPV4_NODE
, &no_bgp_table_map_cmd
);
12146 install_element(BGP_IPV4_NODE
, &aggregate_address_cmd
);
12147 install_element(BGP_IPV4_NODE
, &aggregate_address_mask_cmd
);
12148 install_element(BGP_IPV4_NODE
, &no_aggregate_address_cmd
);
12149 install_element(BGP_IPV4_NODE
, &no_aggregate_address_mask_cmd
);
12151 /* IPv4 multicast configuration. */
12152 install_element(BGP_IPV4M_NODE
, &bgp_table_map_cmd
);
12153 install_element(BGP_IPV4M_NODE
, &bgp_network_cmd
);
12154 install_element(BGP_IPV4M_NODE
, &no_bgp_table_map_cmd
);
12155 install_element(BGP_IPV4M_NODE
, &aggregate_address_cmd
);
12156 install_element(BGP_IPV4M_NODE
, &aggregate_address_mask_cmd
);
12157 install_element(BGP_IPV4M_NODE
, &no_aggregate_address_cmd
);
12158 install_element(BGP_IPV4M_NODE
, &no_aggregate_address_mask_cmd
);
12160 /* IPv4 labeled-unicast configuration. */
12161 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_cmd
);
12162 install_element(VIEW_NODE
, &show_ip_bgp_cmd
);
12163 install_element(VIEW_NODE
, &show_ip_bgp_json_cmd
);
12164 install_element(VIEW_NODE
, &show_ip_bgp_route_cmd
);
12165 install_element(VIEW_NODE
, &show_ip_bgp_regexp_cmd
);
12167 install_element(VIEW_NODE
,
12168 &show_ip_bgp_instance_neighbor_advertised_route_cmd
);
12169 install_element(VIEW_NODE
, &show_ip_bgp_neighbor_routes_cmd
);
12170 install_element(VIEW_NODE
,
12171 &show_ip_bgp_neighbor_received_prefix_filter_cmd
);
12172 #ifdef KEEP_OLD_VPN_COMMANDS
12173 install_element(VIEW_NODE
, &show_ip_bgp_vpn_all_route_prefix_cmd
);
12174 #endif /* KEEP_OLD_VPN_COMMANDS */
12175 install_element(VIEW_NODE
, &show_bgp_afi_vpn_rd_route_cmd
);
12176 install_element(VIEW_NODE
,
12177 &show_ip_bgp_l2vpn_evpn_all_route_prefix_cmd
);
12179 /* BGP dampening clear commands */
12180 install_element(ENABLE_NODE
, &clear_ip_bgp_dampening_cmd
);
12181 install_element(ENABLE_NODE
, &clear_ip_bgp_dampening_prefix_cmd
);
12183 install_element(ENABLE_NODE
, &clear_ip_bgp_dampening_address_cmd
);
12184 install_element(ENABLE_NODE
, &clear_ip_bgp_dampening_address_mask_cmd
);
12187 install_element(ENABLE_NODE
,
12188 &show_ip_bgp_instance_neighbor_prefix_counts_cmd
);
12189 #ifdef KEEP_OLD_VPN_COMMANDS
12190 install_element(ENABLE_NODE
,
12191 &show_ip_bgp_vpn_neighbor_prefix_counts_cmd
);
12192 #endif /* KEEP_OLD_VPN_COMMANDS */
12194 /* New config IPv6 BGP commands. */
12195 install_element(BGP_IPV6_NODE
, &bgp_table_map_cmd
);
12196 install_element(BGP_IPV6_NODE
, &ipv6_bgp_network_cmd
);
12197 install_element(BGP_IPV6_NODE
, &no_bgp_table_map_cmd
);
12199 install_element(BGP_IPV6_NODE
, &ipv6_aggregate_address_cmd
);
12200 install_element(BGP_IPV6_NODE
, &no_ipv6_aggregate_address_cmd
);
12202 install_element(BGP_IPV6M_NODE
, &ipv6_bgp_network_cmd
);
12204 install_element(BGP_NODE
, &bgp_distance_cmd
);
12205 install_element(BGP_NODE
, &no_bgp_distance_cmd
);
12206 install_element(BGP_NODE
, &bgp_distance_source_cmd
);
12207 install_element(BGP_NODE
, &no_bgp_distance_source_cmd
);
12208 install_element(BGP_NODE
, &bgp_distance_source_access_list_cmd
);
12209 install_element(BGP_NODE
, &no_bgp_distance_source_access_list_cmd
);
12210 install_element(BGP_IPV4_NODE
, &bgp_distance_cmd
);
12211 install_element(BGP_IPV4_NODE
, &no_bgp_distance_cmd
);
12212 install_element(BGP_IPV4_NODE
, &bgp_distance_source_cmd
);
12213 install_element(BGP_IPV4_NODE
, &no_bgp_distance_source_cmd
);
12214 install_element(BGP_IPV4_NODE
, &bgp_distance_source_access_list_cmd
);
12215 install_element(BGP_IPV4_NODE
, &no_bgp_distance_source_access_list_cmd
);
12216 install_element(BGP_IPV4M_NODE
, &bgp_distance_cmd
);
12217 install_element(BGP_IPV4M_NODE
, &no_bgp_distance_cmd
);
12218 install_element(BGP_IPV4M_NODE
, &bgp_distance_source_cmd
);
12219 install_element(BGP_IPV4M_NODE
, &no_bgp_distance_source_cmd
);
12220 install_element(BGP_IPV4M_NODE
, &bgp_distance_source_access_list_cmd
);
12221 install_element(BGP_IPV4M_NODE
,
12222 &no_bgp_distance_source_access_list_cmd
);
12223 install_element(BGP_IPV6_NODE
, &bgp_distance_cmd
);
12224 install_element(BGP_IPV6_NODE
, &no_bgp_distance_cmd
);
12225 install_element(BGP_IPV6_NODE
, &ipv6_bgp_distance_source_cmd
);
12226 install_element(BGP_IPV6_NODE
, &no_ipv6_bgp_distance_source_cmd
);
12227 install_element(BGP_IPV6_NODE
,
12228 &ipv6_bgp_distance_source_access_list_cmd
);
12229 install_element(BGP_IPV6_NODE
,
12230 &no_ipv6_bgp_distance_source_access_list_cmd
);
12231 install_element(BGP_IPV6M_NODE
, &bgp_distance_cmd
);
12232 install_element(BGP_IPV6M_NODE
, &no_bgp_distance_cmd
);
12233 install_element(BGP_IPV6M_NODE
, &ipv6_bgp_distance_source_cmd
);
12234 install_element(BGP_IPV6M_NODE
, &no_ipv6_bgp_distance_source_cmd
);
12235 install_element(BGP_IPV6M_NODE
,
12236 &ipv6_bgp_distance_source_access_list_cmd
);
12237 install_element(BGP_IPV6M_NODE
,
12238 &no_ipv6_bgp_distance_source_access_list_cmd
);
12240 install_element(BGP_NODE
, &bgp_damp_set_cmd
);
12241 install_element(BGP_NODE
, &bgp_damp_unset_cmd
);
12242 install_element(BGP_IPV4_NODE
, &bgp_damp_set_cmd
);
12243 install_element(BGP_IPV4_NODE
, &bgp_damp_unset_cmd
);
12245 /* IPv4 Multicast Mode */
12246 install_element(BGP_IPV4M_NODE
, &bgp_damp_set_cmd
);
12247 install_element(BGP_IPV4M_NODE
, &bgp_damp_unset_cmd
);
12249 /* Large Communities */
12250 install_element(VIEW_NODE
, &show_ip_bgp_large_community_list_cmd
);
12251 install_element(VIEW_NODE
, &show_ip_bgp_large_community_cmd
);
12253 /* show bgp ipv4 flowspec detailed */
12254 install_element(VIEW_NODE
, &show_ip_bgp_flowspec_routes_detailed_cmd
);
12256 install_element(VIEW_NODE
, &show_bgp_peerhash_cmd
);
12259 void bgp_route_finish(void)
12264 FOREACH_AFI_SAFI (afi
, safi
) {
12265 bgp_table_unlock(bgp_distance_table
[afi
][safi
]);
12266 bgp_distance_table
[afi
][safi
] = NULL
;